标签:
var cluster = require(‘cluster‘); var http = require(‘http‘); var fs = require("fs"); //var process = require("process"); var num = require("os").cpus().length; if (cluster.isMaster) { if (fs.existsSync("aa.txt")) { fs.unlinkSync("aa.txt"); } // Fork workers. for (var i = 0; i < num; i++) { //console.log("fork " + i); var worker = cluster.fork(); worker.on("message", function(msg) { finish++; if (finish == 8) { console.log("all done."); } this.kill(); }); } var finish = 0; cluster.on(‘exit‘, function(worker, code, signal) { console.log(‘worker ‘ + worker.process.pid + ‘ died‘); }); } else { // Workers can share any TCP connection // In this case its a HTTP server /* console.log("start http server @ " + cluster.worker.process.pid); http.createServer(function(req, res) { console.log("request @ " + cluster.worker.process.pid); res.writeHead(200); setTimeout(function() { res.end("hello world\n @ " + cluster.worker.process.pid); }, 20000); var i = 9999999999999; while(i-- > 0) { } }).listen(8000); */ var i = 99999; var worker = cluster.worker; var fd = fs.openSync("aa.txt", "a+"); //console.log(fd); while (i-- > 0) { //console.log(i + "@" + worker.process.pid); fs.writeSync(fd, i + "@" + worker.process.pid + "\r\n"); } fs.closeSync(fd); process.send("ok"); //worker.kill(); }
标签:
原文地址:http://www.cnblogs.com/eturn/p/4311661.html