Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Using the async and child_process modules, I can create true asynchronous 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

1 Answer 1

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 repeat comment for each function, you copy the first version of the function and just 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() {
        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.onstuff that you were doing to thekbut to thecp.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 –  Alex Mills Jul 1 at 7:53

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.