-
Notifications
You must be signed in to change notification settings - Fork 396
02 如何利用多核CPU
孙正华 edited this page Jul 26, 2018
·
3 revisions
使用 Cluster 模块可以方便地让 Node.js 运行在多进程上。
https://github.com/eshengsky/iBlog2/blob/master/daemon.js#L2-L18
const numCPUs = require('os')
.cpus()
.length;
if (cluster.isMaster) {
let worker;
// 遍历CPU核心数
for (let i = 0; i < numCPUs; i++) {
// 生成新的工作进程运行主模块
worker = cluster.fork();
console.log(`worker:${worker.process.pid} 正在运行...`);
}
} else {
// 运行主模块
require('./bin/www');
}