Running tasks in multiple threads on node.js

I need help with node.js as I am a beginner. I have a data array and you must run functions with this data in multiple threads (using all CPU cores). In my usual languages, I create a threadpool with the number of threads, click on it for some tasks, wait until the end, send more tasks to the queue. But I can not figure out how to do this in nodejs.

PS: I am using the latest 0.8 branch.

+5
source share
1 answer

Nodejs is built to run in one process, but you can spawn other processes. The cluster module uses the fork method of the child_process module, but it is designed to distribute server connections between processes and share the same port.

I think you want exec . Example:

var exec = require('child_process').exec,
child;

var array = ["a", "b", "c", "d", "e"]; // your array of data

var n = array.length; // length
var done = 0; // jobs done
var i = n; // iterator

while(i--) { // reverse while loops are faster
    (function (argument) { // A closure
        child = exec('node otherFunction.js '+argument, // Spawn the process, with an item from your array as an argument.
          function (error, stdout, stderr) {
            if (error === null) {
                done += 1;
                if (done === n) {
                    console.log('Everything is done');
                }
            }
        });
    })(array[i]);
}

The above, of course, is bad code and not even verified, but I think it will work. All you have to do is call the function that you want to call for the elements of the array in otherFunction.js, inside which you will find the arguments to process.argv.

+4
source

All Articles