氟塑料離心泵www.buybeng.com

          jquery教程http://www.software8.co/wzjs/jquery/

          用NodeJS實現(xiàn)集群計算

          Node的單個實例運行在單個的線程中,要充分利用多核系統(tǒng),我們可以運行Node進(jìn)程集群來處理負(fù)載。

          也就是說,如果系統(tǒng)有8核,單個Node實例只能使用其中1核,可以利用cluster包的workers概念來充分利用所有的核。有趣的是,它們可以共享同一個端口。

          該模塊還處于實驗階段。

          [javascript] view plaincopyprint?
          1. var cluster = require('cluster');  
          2. var http = require('http');  
          3. var numCPUs = require('os').cpus().length;  
          4.    
          5. if (cluster.isMaster) {  
          6.   // Fork workers.  
          7.   require('os').cpus().forEach(function(){  
          8.     cluster.fork();  
          9.   });  
          10.   // In case the worker dies!  
          11.   cluster.on('exit'function(worker, code, signal) {  
          12.     console.log('worker ' + worker.process.pid + ' died');  
          13.   });  
          14.   // As workers come up.  
          15.   cluster.on('listening'function(worker, address) {  
          16.     console.log("A worker with #"+worker.id+" is now connected to " +\  
          17.      address.address +\  
          18.     ":" + address.port);  
          19.   });  
          20.   // When the master gets a msg from the worker increment the request count.  
          21.   var reqCount = 0;  
          22.   Object.keys(cluster.workers).forEach(function(id) {  
          23.     cluster.workers[id].on('message',function(msg){  
          24.       if(msg.info && msg.info == 'ReqServMaster'){  
          25.         reqCount += 1;  
          26.       }  
          27.     });  
          28.   });  
          29.   // Track the number of request served.  
          30.   setInterval(function() {  
          31.     console.log("Number of request served = ",reqCount);  
          32.   }, 1000);  
          33.    
          34. else {  
          35.   // Workers can share the same port!  
          36.   require('http').Server(function(req, res) {  
          37.     res.writeHead(200);  
          38.     res.end("Hello from Cluster!");  
          39.     // Notify the master about the request.  
          40.     process.send({ info : 'ReqServMaster' });  
          41.   }).listen(8000);  
          42. }  

          在一個4核的計算機(jī)上,輸出如下:

          [javascript] view plaincopyprint?
          1. Number of request served =  0  
          2. A worker with #2 is now connected to 0.0.0.0:8000  
          3. A worker with #4 is now connected to 0.0.0.0:8000  
          4. A worker with #1 is now connected to 0.0.0.0:8000  
          5. A worker with #3 is now connected to 0.0.0.0:8000  
          6. Number of request served =  0  
          7. ...  
          8. Number of request served =  2  
          9. ..  
          10. Number of request served =  4  
          11. ...  
          12. Number of request served =  6  

          posted on 2012-10-10 10:50 你爸是李剛 閱讀(275) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          技術(shù)網(wǎng)站

          行業(yè)網(wǎng)站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          站長網(wǎng) 氟塑料離心泵 注塑機(jī) 液晶廣告機(jī)
          主站蜘蛛池模板: 长岛县| 枣强县| 北京市| 旬阳县| 岐山县| 瑞安市| 门源| 景宁| 定兴县| 克什克腾旗| 桑植县| 秦安县| 红河县| 上杭县| 奇台县| 五原县| 南安市| 延庆县| 怀安县| 尤溪县| 永川市| 萨迦县| 永昌县| 荣昌县| 延川县| 贵定县| 陕西省| 本溪| 隆子县| 绵竹市| 聂拉木县| 仙桃市| 台山市| 栾川县| 济宁市| 武威市| 农安县| 伊金霍洛旗| 宜兰县| 灵川县| 土默特右旗|