cluster.workers
存储活动工作进程对象的哈希,以 id
字段为键。
这样可以很容易地遍历所有工作进程。
它仅在主进程中可用。
在工作进程断开连接并退出后,工作进程会从 cluster.workers
中删除。
这两个事件之间的顺序无法预先确定。
但是,保证从 cluster.workers
列表中删除发生在最后一个 'disconnect'
或 'exit'
事件触发之前。
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');
}
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.
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');
}