cluster.workers


一个哈希表,用于存储活动的工作对象,以 id 字段为键。这使得遍历所有工作对象变得容易。它仅在主进程中可用。

【A hash that stores the active worker objects, keyed by id field. This makes it easy to loop through all the workers. It is only available in the primary process.】

当一个工作进程已经断开连接并退出后,它会从 cluster.workers 中被移除。这两个事件之间的顺序无法提前确定。然而,可以保证从 cluster.workers 列表中的移除会发生在最后一次 'disconnect''exit' 事件触发之前。

【A worker is removed from cluster.workers after the worker has disconnected and exited. The order between these two events cannot be determined in advance. However, it is guaranteed that the removal from the cluster.workers list happens before the last 'disconnect' or 'exit' event is emitted.】

import cluster from 'node:cluster';

for (const worker of Object.values(cluster.workers)) {
  worker.send('big announcement to all workers');
}const cluster = require('node:cluster');

for (const worker of Object.values(cluster.workers)) {
  worker.send('big announcement to all workers');
}