Messtone LLC Manages(CJS•ESM):

Cluster The cluster module allows easy creation of child process that all share server ports.CJS•ESM import cluster from ‘node : cluster’; import http from ‘node : http’;import {availableParallelism} from ‘node : os’;import process from ‘node : process’; const numCPUs = availableParallelism( )if(cluster.isPrimary){console.log(Primary ${process.pid} is running); //Fork works.for(let i = 0; i <numCPUs; i ++){cluster.Fork( );} cluster.on('exit',(worker,code,signal)=>{console.log(worker ${worker.process.pid} died);});}else{//Workers can share any TCP connection //In this case it is an HTTP server http.createServer((req,res)=>{res.writeHead(200);res.end('hello world\n');}).listen(8000);console.log(Worker ${process.pid} started`);} Running Node.js will now share port 8000 between the workers:$node server.js Primary 3596 is running Worker 4324 started Worker 4520 started Worker 6056 Worker 5644 started cluster.om(‘disconnect’) event,but specific to this to worker. cluster.fork( ).on(‘disconnect’,( )=>{//Worker has disconnected});

Leave a comment