When using cluster.fork
, is there a way to check which number the worker
created is within the worker itself?
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
}
The cluster.worker.id should be an incremental number.
if (cluster.isWorker) {
console.log(cluster.worker.id); // 0, 1, 2 etc.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.