Skip to content

Instantly share code, notes, and snippets.

@ilkovich
Created September 22, 2015 14:54
Show Gist options
  • Select an option

  • Save ilkovich/a4f11be91984c5f5f0ab to your computer and use it in GitHub Desktop.

Select an option

Save ilkovich/a4f11be91984c5f5f0ab to your computer and use it in GitHub Desktop.
var cluster = require('cluster')
, worker
, http = require('http')
;
function log(msg) {
process.stdout.write(msg);
}
if(cluster.isMaster) {
cluster.setupMaster({ silent: true }); // Keep cluster from automatically grabbing stdin/out/err
cluster.fork();
cluster.on('fork', function(worker) {
worker.process.stdout.on('data', function(chunk) {
log('out: '+chunk);
});
worker.process.stderr.on('data', function(chunk) {
log('err: '+chunk);
});
});
//arbitrarily keep the master process open.
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
} else {
for(var i=0; i<100; i++) {
if(Math.random()*100 < i)
console.log(i);
else
console.error(i);
}
process.exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment