2
\$\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);
asked May 28, 2015 at 3:16
\$\endgroup\$

1 Answer 1

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.

Ext3h
2,83913 silver badges17 bronze badges
answered Jun 28, 2015 at 22:15
\$\endgroup\$
2
  • \$\begingroup\$ cores just means CPU \$\endgroup\$ Commented Jul 1, 2015 at 7:53
  • \$\begingroup\$ can we increase loop count upto 5000 or more to fork cp? \$\endgroup\$ Commented Mar 9, 2016 at 10:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.