Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
    
cores just means CPU – Alexander Mills Jul 1 '15 at 7:53
    
can we increase loop count upto 5000 or more to fork cp? – Suresh Mahawar Mar 9 at 10:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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