-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterTest.js
41 lines (38 loc) · 1.1 KB
/
clusterTest.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
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
var receivenum = 0;
if(cluster.isMaster){
console.log('Master: Master is running');
// Fork workers
var workers = new Array(numCPUs);
for(let i = 0; i < numCPUs; i ++){
workers[i] = cluster.fork();
workers[i].on('message',(m)=>{
console.log("Master: message received " +(i+1) +" chat :" + m['chat']);
receivenum++;
if(receivenum == numCPUs){
// for(let j = 0; j <numCPUs;j++){
// workers[i].send()
// }
process.exit();
}
});
}
setTimeout(function(){
for(let i = 0 ; i < numCPUs;i++){
console.log("Master: message for the " + i + " sended");
workers[i].send({id: i+1});
}
},3000);
} else{
process.on('message',(m)=>{
console.log("worker" + cluster.worker.id+ ": received msg : " + m["id"]);
process.send({chat: "hey master worker" + cluster.worker.id + " get the message!"});
});
process.on('exit',(m,s)=>{
console.log("worker" + cluster.worker.id+ ": received msg to die ");
process.exit();
})
console.log("worker"+cluster.worker.id+"started ");
}