Skip to content

Instantly share code, notes, and snippets.

@bugb
Last active July 14, 2020 07:33
Show Gist options
  • Select an option

  • Save bugb/405f2e0dd6bd63800527209e7d4b68f8 to your computer and use it in GitHub Desktop.

Select an option

Save bugb/405f2e0dd6bd63800527209e7d4b68f8 to your computer and use it in GitHub Desktop.
nodejs multithread
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(deadWorker, code, signal) {
var worker = cluster.fork();
const newPID = worker.process.pid;
const oldPID = deadWorker.process.pid;
console.log(`worker ${oldPID} exited.`);
console.log(`worker ${newPID} spawned.`);
});
} else {
// your app go here
console.log(2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment