Created
December 1, 2011 14:51
-
-
Save billywhizz/1417302 to your computer and use it in GitHub Desktop.
cluster example
This file contains hidden or 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
var cluster = require('cluster'); | |
function fib (n) { | |
if (n < 2) { | |
return 1; | |
} | |
else { | |
return fib(n-2) + fib(n-1); | |
} | |
} | |
var i= 0; | |
var n= 35; | |
function srv (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain', 'Content-Length': '0'}); | |
fib(n); | |
res.end(); | |
} | |
if (cluster.isMaster) { | |
var numCPUs = process.argv[3] || 1; | |
for (var i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('death', function(worker) { | |
console.log('worker ' + worker.pid + ' died'); | |
}); | |
} else { | |
var port= + process.argv[2] || 1234; | |
require('http').createServer(srv).listen(port); | |
console.log('Fibonacci server (NO THREADS) listening: ' + port); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment