Nodejs is built to run in one process, but you can spawn other processes. The cluster module uses the fork method of the child_process module, but it is designed to distribute server connections between processes and share the same port.
I think you want exec . Example:
var exec = require('child_process').exec,
child;
var array = ["a", "b", "c", "d", "e"];
var n = array.length;
var done = 0;
var i = n;
while(i--) {
(function (argument) {
child = exec('node otherFunction.js '+argument,
function (error, stdout, stderr) {
if (error === null) {
done += 1;
if (done === n) {
console.log('Everything is done');
}
}
});
})(array[i]);
}
The above, of course, is bad code and not even verified, but I think it will work. All you have to do is call the function that you want to call for the elements of the array in otherFunction.js, inside which you will find the arguments to process.argv.
source
share