-
Notifications
You must be signed in to change notification settings - Fork 14
/
worker_test.js
42 lines (38 loc) · 952 Bytes
/
worker_test.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
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads')
const TIMEOUT = 1000
const threadCount = 3
const threads = new Set()
for (let i = 0; i < threadCount; i++) {
setTimeout(() => {
threads.add(
new Worker(`${process.cwd()}/worker.js`)
)
}, i * TIMEOUT)
}
setTimeout(() => {
console.log('threads : ', threads.size)
for (let worker of threads) {
worker.on('message', msg => {
console.log('msg : ', msg)
})
worker.on('error', err => {
console.log('worker err : ', err)
threads.delete(worker)
if (threads.size === 0) {
console.log('done')
process.exit(0)
}
})
worker.on('exit', () => {
console.log('worker done')
threads.delete(worker)
if (threads.size === 0) {
console.log('done')
process.exit(0)
}
})
}
}, (threadCount * TIMEOUT) + 100)
setInterval(() => {
console.log('.')
}, 1000)