1
\$\begingroup\$

Using the async and child_process modules, I can create true "parallel" processing. My question is, how can I ensure that it is running on multiple cores?

var cp = require('child_process');
var async = require('async');

async.parallel([

    function(callback){
        console.log('1');
        var k = cp.fork('./doOther.js',['01']); //how can I ensure the child process k runs on a different core than this process?
        k.on('message',function(msg){
            console.log(msg);
            callback();
        });
    },
    function(callback){
        console.log('2');
        var k = cp.fork('./doOther.js',['02']); //how can I ensure the child process k runs on a different core than this process?
        k.on('message',function(msg){
            console.log(msg);
            callback();
        });
    },
    function(callback){
        console.log('3');
        var k = cp.fork('./doOther.js',['03']); //how can I ensure the child process k runs on a different core than this process?
        k.on('message',function(msg){
            console.log(msg);
            callback();
        });

    },
    function(callback){
        console.log('4');
        var k = cp.fork('./doOther.js',['04']); //how can I ensure the child process k runs on a different core than this process?
        k.on('message',function(msg){
            console.log(msg);
            callback();
        });

    }

],function(){

    console.log('done.');
    process.exit(0);

});

The above script interacts with IPC (inter-process comm) with this script:

//doOther.js, a simple script that interacts with the script above

    process.on('message', function(m) {
        console.log('CHILD got message:', m);
    });

    setTimeout(function(){
        process.send({ foo: process.argv[2] });
    },1000);
\$\endgroup\$
2
\$\begingroup\$

Notice a pattern in the array of your async.parallel call? All the functions are the same except for a single number.

And, judging by the repeated comment for each function, you just copied the first version of the function and changed that one number.

You should create a loop that pushes functions into the array, changing that single number each time.

for(var i = 0; i < 4; i++) {
    array.push(function(callback) {
        console.log(i + 1);
        var k = cp.fork('./doOther.js',['0' + (i + 1)]);
        k.on('message',function(msg){
            console.log(msg);
            callback();
        });
    }
}

As another tip, if you aren't going to use k again, don't store cp.form(...,...); in a variable; just continue the .on stuff that you were doing to the k but to the cp.form(...,...).

That would look like this:

cp.fork('./doOther.js', ['0' + (i + 1)]).on('message', function(msg) {
    console.log(msg)
    callback();
});

Other than that, I'm not too familiar with node.js and what a core is, so I can't help you on that part.

\$\endgroup\$
  • \$\begingroup\$ cores just means CPU \$\endgroup\$ – Alexander Mills Jul 1 '15 at 7:53
  • \$\begingroup\$ can we increase loop count upto 5000 or more to fork cp? \$\endgroup\$ – Suresh Mahawar Mar 9 '16 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.