-
Notifications
You must be signed in to change notification settings - Fork 0
/
childNodes.js
41 lines (38 loc) · 996 Bytes
/
childNodes.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 { fork } = require('child_process');
const heavyCommand = i => {
const now = Date.now();
console.log(`${i} start`);
while (Date.now() - now < 5000) {};
console.log(`${i} finish`);
if (process.send) process.send('ok');
}
const start = withChildNodes => {
if (withChildNodes) {
if (process.argv.length === 3) {
console.log(`start ${new Date}`);
const childs = parseInt(process.argv[2]);
let finished = 0;
for (let i = 0; i < childs; i++) {
const subProc = fork(__filename);
subProc.on("message", m => {
if (++finished === childs) {
console.log(`finish ${new Date}`);
process.exit(0);
}
});
subProc.send(i);
}
} else {
process.on('message', i => {
heavyCommand(i);
})
}
} else {
console.log(`start ${new Date}`);
for (let i = 0; i < 5; i++) {
heavyCommand(i);
}
console.log(`finish ${new Date}`);
}
}
start(false);