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);