繁体   English   中英

Node.js - cluster.fork() - 如何检查工作人员中的工作人员数量?

[英]Node.js - cluster.fork() - how to check number of worker within the worker?

使用cluster.fork时,有没有办法检查创建的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.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM