-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmain.ts
42 lines (36 loc) · 1.12 KB
/
main.ts
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
import * as cluster from 'cluster'
import * as OS from 'os'
import chalk from 'chalk'
import app from './koa'
// 获取CPU数量
const nums = OS.cpus().length
// 当前如果是主线程
if (cluster.isMaster) {
// 循环 fork 任务
for (let i = 0; i < nums; i++) {
const worker = cluster.fork()
worker.on('online', () => {
worker.send('worker is online')
console.log(chalk.cyanBright(`worker is online`))
})
}
// 打印当前主线程
console.log(chalk.green(`主线程运行在${process.pid}`))
// 监听 exit 事件
cluster.on('exit', (worker: cluster.Worker, code: number, signal: string) => {
console.log(chalk.redBright(`工作线程${worker.process.pid}已退出`))
})
cluster.on('message', (worker, msg, handle) => {
console.log(handle)
console.log(chalk.red(worker.id + ''), msg)
})
// 当前如果是工作线程
} else {
app.listen(2001)
// 打印当前子线程
// process.send({ cmd: '1', data: 'Hello Master' })
process.on('message', (msg) => {
console.log(chalk.cyanBright('Message from Master ' + msg))
process.send('Message from Master ' + msg)
})
}