@@ -17,10 +17,10 @@ server ports.
1717``` mjs
1818import cluster from ' node:cluster' ;
1919import http from ' node:http' ;
20- import { cpus } from ' node:os' ;
20+ import { availableParallelism } from ' node:os' ;
2121import process from ' node:process' ;
2222
23- const numCPUs = cpus (). length ;
23+ const numCPUs = availableParallelism () ;
2424
2525if (cluster .isPrimary ) {
2626 console .log (` Primary ${ process .pid } is running` );
@@ -48,7 +48,7 @@ if (cluster.isPrimary) {
4848``` cjs
4949const cluster = require (' node:cluster' );
5050const http = require (' node:http' );
51- const numCPUs = require (' node:os' ).cpus (). length ;
51+ const numCPUs = require (' node:os' ).availableParallelism () ;
5252const process = require (' node:process' );
5353
5454if (cluster .isPrimary ) {
@@ -273,7 +273,7 @@ process of the number of HTTP requests received by the workers:
273273``` mjs
274274import cluster from ' node:cluster' ;
275275import http from ' node:http' ;
276- import { cpus } from ' node:os' ;
276+ import { availableParallelism } from ' node:os' ;
277277import process from ' node:process' ;
278278
279279if (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
508508import cluster from ' node:cluster' ;
509509import http from ' node:http' ;
510- import { cpus } from ' node:os' ;
510+ import { availableParallelism } from ' node:os' ;
511511import process from ' node:process' ;
512512
513- const numCPUs = cpus (). length ;
513+ const numCPUs = availableParallelism () ;
514514
515515if (cluster .isPrimary ) {
516516 console .log (` Primary ${ process .pid } is running` );
@@ -540,7 +540,7 @@ if (cluster.isPrimary) {
540540``` cjs
541541const cluster = require (' node:cluster' );
542542const http = require (' node:http' );
543- const numCPUs = require (' node:os' ).cpus (). length ;
543+ const numCPUs = require (' node:os' ).availableParallelism () ;
544544const process = require (' node:process' );
545545
546546if (cluster .isPrimary ) {
0 commit comments