Skip to content

Instantly share code, notes, and snippets.

@jamlfy
Created June 29, 2013 00:57
Show Gist options
  • Save jamlfy/5889188 to your computer and use it in GitHub Desktop.
Save jamlfy/5889188 to your computer and use it in GitHub Desktop.
Using all machine or pc
var cluster = require('cluster');
var http = require('http');
var free = 250000000;
var sizeWoker = 157286400;
var NumberWorkers = Math.round( ( os.freemem() - free ) / sizeWoker );
if (cluster.isMaster) {
for (var i = 0; i < NumberWorkers; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
});
} else {
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