Created
May 11, 2012 18:55
-
-
Save collinschaafsma/2661685 to your computer and use it in GitHub Desktop.
node cluster
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
// Fast | |
var http = require('http'); | |
http.Server(function(req, res) { | |
res.writeHead(200); | |
res.end("hello world\n"); | |
}).listen(8000); | |
// Faster | |
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('death', function(worker) { | |
console.log('worker ' + worker.pid + ' died'); | |
}); | |
} else { | |
// Worker processes have a http server. | |
http.Server(function(req, res) { | |
res.writeHead(200); | |
res.end("hello world\n"); | |
}).listen(8000); | |
} | |
// Both fall over though at about the same rate under lots of load :( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment