-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample.cluster.js
47 lines (40 loc) · 1020 Bytes
/
sample.cluster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var cluster = require('cluster'),
async = require('async'),
utils = require('./lib/utils');
function send(range, callback) {
cluster.fork({
start: range.start,
end: range.end
}).on('message', function (msg) {
callback(null, msg);
});
}
function handle(msg) {
utils.log(msg.start, msg.end, msg.result);
}
if (cluster.isMaster) {
async.parallel([
async.apply(send, utils.range(0)),
async.apply(send, utils.range(1)),
async.apply(send, utils.range(2)),
async.apply(send, utils.range(3)),
async.apply(send, utils.range(4))
], function (err, results) {
results.forEach(handle);
});
}
else {
var wordify = require('./lib/wordify'),
fourism = require('./lib/fourism');
var counts = {};
for (var x = process.env.start; x < process.env.end; x++) {
var turns = fourism(wordify(x));
counts[turns] = ++counts[turns] || 1;
}
process.send({
start: process.env.start,
end: process.env.end,
result: counts
});
process.exit();
}