-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcommunication.js
54 lines (48 loc) · 1.45 KB
/
communication.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
48
49
50
51
52
53
54
var cluster = require('cluster'),
factorial = function factorial(num) {
if(num === 1 || num === 0) return 1;
else return num * factorial(num-1);
};
if(cluster.isMaster) {
var numWorkers = require('os').cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for(var i = 0; i < numWorkers; i++) {
var worker = cluster.fork();
worker.on('message', function(message) {
console.log(message.from + ': ' + message.type + ' ' + message.data.number + ' = ' + message.data.result);
});
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
for(var wid in cluster.workers) {
cluster.workers[wid].send({
type: 'factorial',
from: 'master',
data: {
number: Math.floor(Math.random() * 50)
}
});
}
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
var worker = cluster.fork();
worker.on('message', function(message) {
console.log(message.from + ': ' + message.type + ' ' + message.data.number + ' = ' + message.data.result);
});
});
} else {
process.on('message', function(message) {
if(message.type === 'factorial') {
process.send({
type:'factorial',
from: 'Worker ' + process.pid,
data: {
number: message.data.number,
result: factorial(message.data.number)
}
});
}
});
}