Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created June 17, 2014 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanoats/6f7e5c76bd02ac37d218 to your computer and use it in GitHub Desktop.
Save ivanoats/6f7e5c76bd02ac37d218 to your computer and use it in GitHub Desktop.
'use strict';
var cluster = require('cluster');
var os = require('os');
var i;
// Am I the master of the cluster?
// YES - spawn the workers
// NO - run the work
if (cluster.isMaster) {
console.log('I am the master!');
console.log(process.pid);
for(i=0; i < os.cpus().length; i++) {
cluster.fork();
}
cluster.on('exit', function(worker){
cluster.fork();
});
} else {
console.log('I am a worker');
console.log(process.pid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment