Last active
July 14, 2020 07:33
-
-
Save bugb/405f2e0dd6bd63800527209e7d4b68f8 to your computer and use it in GitHub Desktop.
nodejs multithread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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