首页 > 代码库 > nodejs中cluster的运用

nodejs中cluster的运用

nodejs中cluster的运用

by 伍雪颖

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
	for (var i=0;i<numCPUs;i++) {
		cluster.fork();
	}
	cluster.on('exit',function(worker,code,signal) {
		console.log('worker '+worker.process.pid+ ' died');
	});
}else {
	http.createServer(function(req,res) {
		res.writeHead(200);
		res.end('hello world\n');
	}).listen(8000);
}


nodejs中cluster的运用