Created
December 9, 2011 00:49
-
-
Save mmalecki/1449542 to your computer and use it in GitHub Desktop.
Example of multiprocessing in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'), | |
fork = require('child_process').fork; | |
var N = 8, | |
M = 128; | |
// Generate data set | |
console.log('Generating data sets...'); | |
var dataSet = [], row = []; | |
for (var i = 0; i < N; i++) { | |
for (var j = 0; j < M; j++) | |
row.push(Math.random() * Math.PI); | |
dataSet.push(row); | |
row = []; | |
} | |
console.log('Finished generating data sets'); | |
console.log('Spawning slave processes'); | |
var completed = 0, results = []; | |
for (var i = 0; i < N; i++) { | |
// Spawn up node processes and tell them to do some number crunching | |
(function (i) { | |
// This function is here because loops don't create a new scope | |
var child = fork(path.join(__dirname, 'slave.js')); | |
child.send(dataSet[i]); | |
child.once('message', function (m) { | |
console.log('Got results from child ' + i.toString()); | |
results[i] = m; | |
if (++completed == N) { | |
console.log('Got results from all children. Quitting.'); | |
process.exit(); | |
} | |
child.kill(); | |
}); | |
})(i); | |
} | |
console.log('Finished spawning slave processes'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('message', function (m) { | |
// Got a job, start processing | |
process.send(m.map(function (item) { return Math.sin(item); })); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment