氟塑料離心泵www.buybeng.com

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

          用NodeJS實現集群計算

          Node的單個實例運行在單個的線程中,要充分利用多核系統,我們可以運行Node進程集群來處理負載。

          也就是說,如果系統有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核的計算機上,輸出如下:

          [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)  編輯  收藏


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


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          技術網站

          行業網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          站長網 氟塑料離心泵 注塑機 液晶廣告機
          主站蜘蛛池模板: 贵港市| 庄河市| 论坛| 印江| 鹰潭市| 乐山市| 博兴县| 白沙| 深圳市| 南安市| 东丰县| 庆阳市| 柏乡县| 海丰县| 巩留县| 汤阴县| 通江县| 湟中县| 静海县| 石城县| 红安县| 育儿| 图木舒克市| 年辖:市辖区| 诸暨市| 苏州市| 滨州市| 桦甸市| 鸡泽县| 鹤庆县| 灌阳县| 万年县| 西藏| 五华县| 丽水市| 涪陵区| 扎兰屯市| 隆安县| 深水埗区| 霞浦县| 屏山县|