X Tutup
The Wayback Machine - https://web.archive.org/web/20220320071927/https://github.com/nodejs/node/commit/b951764c32
Skip to content
Permalink
Browse files
doc: modernize and simplify cluster example
PR-URL: #41626
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
tniessen authored and BethGriggs committed Jan 24, 2022
1 parent d33d189 commit b951764c32423bb3c7e79ca14357838112d9bce1
Showing with 4 additions and 16 deletions.
  1. +4 −16 doc/api/cluster.md
@@ -1074,29 +1074,17 @@ list happens before the last `'disconnect'` or `'exit'` event is emitted.
```mjs
import cluster from 'cluster';
// Go through all workers
function eachWorker(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
for (const worker of Object.values(cluster.workers)) {
worker.send('big announcement to all workers');
});
}
```

```cjs
const cluster = require('cluster');
// Go through all workers
function eachWorker(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
for (const worker of Object.values(cluster.workers)) {
worker.send('big announcement to all workers');
});
}
```

Using the worker's unique id is the easiest way to locate the worker.

0 comments on commit b951764

Please sign in to comment.
X Tutup