Skip to content

Commit 9251dce

Browse files
deokjinkimRafaelGSS
authored andcommitted
doc: use os.availableParallelism() in async_context and cluster
Refs: #45895 PR-URL: #45979 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent bc43922 commit 9251dce

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

doc/api/async_context.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ This pool could be used as follows:
762762
import WorkerPool from './worker_pool.js';
763763
import os from 'node:os';
764764

765-
const pool = new WorkerPool(os.cpus().length);
765+
const pool = new WorkerPool(os.availableParallelism());
766766

767767
let finished = 0;
768768
for (let i = 0; i < 10; i++) {
@@ -778,7 +778,7 @@ for (let i = 0; i < 10; i++) {
778778
const WorkerPool = require('./worker_pool.js');
779779
const os = require('node:os');
780780

781-
const pool = new WorkerPool(os.cpus().length);
781+
const pool = new WorkerPool(os.availableParallelism());
782782

783783
let finished = 0;
784784
for (let i = 0; i < 10; i++) {

doc/api/cluster.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ server ports.
1717
```mjs
1818
import cluster from 'node:cluster';
1919
import http from 'node:http';
20-
import { cpus } from 'node:os';
20+
import { availableParallelism } from 'node:os';
2121
import process from 'node:process';
2222

23-
const numCPUs = cpus().length;
23+
const numCPUs = availableParallelism();
2424

2525
if (cluster.isPrimary) {
2626
console.log(`Primary ${process.pid} is running`);
@@ -48,7 +48,7 @@ if (cluster.isPrimary) {
4848
```cjs
4949
const cluster = require('node:cluster');
5050
const http = require('node:http');
51-
const numCPUs = require('node:os').cpus().length;
51+
const numCPUs = require('node:os').availableParallelism();
5252
const process = require('node:process');
5353

5454
if (cluster.isPrimary) {
@@ -273,7 +273,7 @@ process of the number of HTTP requests received by the workers:
273273
```mjs
274274
import cluster from 'node:cluster';
275275
import http from 'node:http';
276-
import { cpus } from 'node:os';
276+
import { availableParallelism } from 'node:os';
277277
import process from 'node:process';
278278

279279
if (cluster.isPrimary) {
@@ -292,7 +292,7 @@ if (cluster.isPrimary) {
292292
}
293293

294294
// Start workers and listen for messages containing notifyRequest
295-
const numCPUs = cpus().length;
295+
const numCPUs = availableParallelism();
296296
for (let i = 0; i < numCPUs; i++) {
297297
cluster.fork();
298298
}
@@ -335,7 +335,7 @@ if (cluster.isPrimary) {
335335
}
336336

337337
// Start workers and listen for messages containing notifyRequest
338-
const numCPUs = require('node:os').cpus().length;
338+
const numCPUs = require('node:os').availableParallelism();
339339
for (let i = 0; i < numCPUs; i++) {
340340
cluster.fork();
341341
}
@@ -507,10 +507,10 @@ because of exiting or being signaled). Otherwise, it returns `false`.
507507
```mjs
508508
import cluster from 'node:cluster';
509509
import http from 'node:http';
510-
import { cpus } from 'node:os';
510+
import { availableParallelism } from 'node:os';
511511
import process from 'node:process';
512512

513-
const numCPUs = cpus().length;
513+
const numCPUs = availableParallelism();
514514

515515
if (cluster.isPrimary) {
516516
console.log(`Primary ${process.pid} is running`);
@@ -540,7 +540,7 @@ if (cluster.isPrimary) {
540540
```cjs
541541
const cluster = require('node:cluster');
542542
const http = require('node:http');
543-
const numCPUs = require('node:os').cpus().length;
543+
const numCPUs = require('node:os').availableParallelism();
544544
const process = require('node:process');
545545

546546
if (cluster.isPrimary) {

0 commit comments

Comments
 (0)