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);
1 Answer 1
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.
-
\$\begingroup\$ cores just means CPU \$\endgroup\$Alexander Mills– Alexander Mills2015年07月01日 07:53:41 +00:00Commented Jul 1, 2015 at 7:53
-
\$\begingroup\$ can we increase loop count upto 5000 or more to fork cp? \$\endgroup\$Suresh Mahawar– Suresh Mahawar2016年03月09日 10:00:21 +00:00Commented Mar 9, 2016 at 10:00
Explore related questions
See similar questions with these tags.