node-compute-clusterNode.js 扩展
node-compute-cluster 是一个让 Node.js 支持跨进程分布式计算的扩展。
主程序:
const computecluster = require('compute-cluster'); // allocate a compute cluster var cc = new computecluster({ module: './worker.js' }); var toRun = 10 // then you can perform work in parallel for (var i = 0; i < toRun; i++) { cc.enqueue({}, function(err, r) { if (err) console.log("an error occured:", err); else console.log("it's nice:", r); if (--toRun === 0) cc.exit(); }); };
worker.js:
process.on('message', function(m) { for (var i = 0; i < 100000000; i++); process.send('complete'); });
评论