Node.js - cluster.fork() - 如何检查工人中工人的数量?
Node.js - cluster.fork() - how to check number of worker within the worker?
当使用 cluster.fork
时,有没有办法检查 worker
创建的哪个数字在 worker 本身中?
if(cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < cores.length; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
}
else{
var number = CLUSTER_NUM; //Get the spawned number of the worker
}
cluster.worker.id应该是递增的数字。
if (cluster.isWorker) {
console.log(cluster.worker.id); // 0, 1, 2 etc.
}
当使用 cluster.fork
时,有没有办法检查 worker
创建的哪个数字在 worker 本身中?
if(cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers
for (let i = 0; i < cores.length; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
}
else{
var number = CLUSTER_NUM; //Get the spawned number of the worker
}
cluster.worker.id应该是递增的数字。
if (cluster.isWorker) {
console.log(cluster.worker.id); // 0, 1, 2 etc.
}