Created
May 27, 2012 11:05
-
-
Save tommedema/2805210 to your computer and use it in GitHub Desktop.
This file contains 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
var cluster = require('cluster'), | |
nrCPUs = require('os').cpus().length; | |
//when we are the master | |
if (cluster.isMaster) { | |
//fork workers (one for each CPU) | |
for (var i = 0; i < nrCPUs; i++) { | |
spawnWorker(); | |
} | |
//respawn on death | |
cluster.on('death', respawnWorker); | |
//stop respawning when we are quitting | |
process.once('exit', function() { | |
cluster.removeListener('death', respawnWorker); | |
}); | |
function respawnWorker() { | |
//respawn worker | |
spawnWorker(); | |
} | |
function spawnWorker() { | |
//fork a worker | |
var worker = cluster.fork(); | |
//tell worker to quit on exit | |
process.on('exit', function() { | |
console.log('I NEVER FIRE'); //why does this never fire? | |
worker.send('SIGQUIT'); | |
}); | |
} | |
} | |
else { | |
//load the worker logic here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment