UNPKG

1.08 kBPlain TextView Raw
1// const cluster = require('cluster')
2//
3// if (cluster.isMaster) {
4// const numCPUs = require('os').cpus().length
5// global['a'] = 1
6// console.log('numCPUs=>>>', numCPUs)
7// for (let i = 0; i < numCPUs; i++) {
8// // 创建子进程模拟线上请求的隔离方便global变量的使用保持与FC环境的一致性
9// cluster.fork()
10// }
11// cluster.on('exit', function (worker, code, signal) {
12// // console.log('Worker %d died with code/signal %s. Restarting worker...', worker.process.pid, signal || code);
13// cluster.fork();
14// })
15// } else {
16// if (!global['a']) {
17// global['a'] = 0
18// }
19// global['a'] += 1
20// console.log('=>>>>', global['a'], new Date())
21// const express = require('express')
22// // Workers share the TCP connection in this server
23// const app = express()
24//
25// app.get('/', function (req, res) {
26// res.send('Hello World!' + new Date());
27// process.exit()
28// })
29//
30// // All workers use this port
31// app.listen(8080)
32// }