Skip to content

Instantly share code, notes, and snippets.

@rofrol
Created March 21, 2014 09:53
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 rofrol/9682925 to your computer and use it in GitHub Desktop.
Save rofrol/9682925 to your computer and use it in GitHub Desktop.
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.pid + ' died');
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment