From 9251dce8b239114d93aa2b69b29ae8b29f3462d2 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Wed, 28 Dec 2022 20:45:10 +0900 Subject: [PATCH] doc: use `os.availableParallelism()` in async_context and cluster Refs: https://github.com/nodejs/node/pull/45895 PR-URL: https://github.com/nodejs/node/pull/45979 Reviewed-By: Colin Ihrig Reviewed-By: Moshe Atlow Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Luigi Pinca --- doc/api/async_context.md | 4 ++-- doc/api/cluster.md | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/api/async_context.md b/doc/api/async_context.md index c0806c3f2330cc..ce63e639f6ee59 100644 --- a/doc/api/async_context.md +++ b/doc/api/async_context.md @@ -762,7 +762,7 @@ This pool could be used as follows: import WorkerPool from './worker_pool.js'; import os from 'node:os'; -const pool = new WorkerPool(os.cpus().length); +const pool = new WorkerPool(os.availableParallelism()); let finished = 0; for (let i = 0; i < 10; i++) { @@ -778,7 +778,7 @@ for (let i = 0; i < 10; i++) { const WorkerPool = require('./worker_pool.js'); const os = require('node:os'); -const pool = new WorkerPool(os.cpus().length); +const pool = new WorkerPool(os.availableParallelism()); let finished = 0; for (let i = 0; i < 10; i++) { diff --git a/doc/api/cluster.md b/doc/api/cluster.md index f904272f62bdc6..93723114ab8835 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -17,10 +17,10 @@ server ports. ```mjs import cluster from 'node:cluster'; import http from 'node:http'; -import { cpus } from 'node:os'; +import { availableParallelism } from 'node:os'; import process from 'node:process'; -const numCPUs = cpus().length; +const numCPUs = availableParallelism(); if (cluster.isPrimary) { console.log(`Primary ${process.pid} is running`); @@ -48,7 +48,7 @@ if (cluster.isPrimary) { ```cjs const cluster = require('node:cluster'); const http = require('node:http'); -const numCPUs = require('node:os').cpus().length; +const numCPUs = require('node:os').availableParallelism(); const process = require('node:process'); if (cluster.isPrimary) { @@ -273,7 +273,7 @@ process of the number of HTTP requests received by the workers: ```mjs import cluster from 'node:cluster'; import http from 'node:http'; -import { cpus } from 'node:os'; +import { availableParallelism } from 'node:os'; import process from 'node:process'; if (cluster.isPrimary) { @@ -292,7 +292,7 @@ if (cluster.isPrimary) { } // Start workers and listen for messages containing notifyRequest - const numCPUs = cpus().length; + const numCPUs = availableParallelism(); for (let i = 0; i < numCPUs; i++) { cluster.fork(); } @@ -335,7 +335,7 @@ if (cluster.isPrimary) { } // Start workers and listen for messages containing notifyRequest - const numCPUs = require('node:os').cpus().length; + const numCPUs = require('node:os').availableParallelism(); for (let i = 0; i < numCPUs; i++) { cluster.fork(); } @@ -507,10 +507,10 @@ because of exiting or being signaled). Otherwise, it returns `false`. ```mjs import cluster from 'node:cluster'; import http from 'node:http'; -import { cpus } from 'node:os'; +import { availableParallelism } from 'node:os'; import process from 'node:process'; -const numCPUs = cpus().length; +const numCPUs = availableParallelism(); if (cluster.isPrimary) { console.log(`Primary ${process.pid} is running`); @@ -540,7 +540,7 @@ if (cluster.isPrimary) { ```cjs const cluster = require('node:cluster'); const http = require('node:http'); -const numCPUs = require('node:os').cpus().length; +const numCPUs = require('node:os').availableParallelism(); const process = require('node:process'); if (cluster.isPrimary) {