node.js中cluster的使用教程
更新時間:2017年06月09日 08:43:22 作者:銀狐被占用
這篇文章主要介紹了node.js中cluster的使用教程,分別介紹使用NODE中cluster利用多核CPU、通過消息傳遞來監(jiān)控工作進程狀態(tài)以及終止進程等功能,給出了詳細的示例代碼供大家參考學習,需要的朋友們下面來一起看看吧
本文主要給大家介紹了關(guān)于node.js中cluster使用的相關(guān)教程,分享出來供大家參考學習,下面來看看詳細的介紹:
一、使用NODE中cluster利用多核CPU
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// 創(chuàng)建工作進程
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
cluster.fork();//重啟子進程
});
} else {
// 工作進程創(chuàng)建http 服務器
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
} 二、通過消息傳遞來監(jiān)控工作進程狀態(tài)
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var rssWarn = (12 * 1024 * 1024)
, heapWarn = (10 * 1024 * 1024)
if(cluster.isMaster) {
for(var i=0; i<numCPUs; i++) {
var worker = cluster.fork();
worker.on('message', function(m) {
if (m.memory) {
console.log(m.memory.rss,rssWarn)
if(m.memory.rss > rssWarn) {
console.log('Worker ' + m.process + ' using too much memory.')
}
}
})
}
} else {
// 服務器
http.createServer(function(req,res) {
res.writeHead(200);
res.end('hello world\n')
}).listen(8000)
// 每秒報告一次狀態(tài)
setInterval(function report(){
process.send({memory: process.memoryUsage(), process: process.pid});
}, 1000)
} 三、終止進程
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var rssWarn = (50 * 1024 * 1024)
, heapWarn = (50 * 1024 * 1024)
var workers = {}
if(cluster.isMaster) {
for(var i=0; i<numCPUs; i++) {
createWorker()
}
setInterval(function() {
var time = new Date().getTime()
for(pid in workers) {
if(workers.hasOwnProperty(pid) &&
workers[pid].lastCb + 5000 < time) {
console.log('Long running worker ' + pid + ' killed')
workers[pid].worker.kill()
delete workers[pid]
createWorker()
}
}
}, 1000)
} else {
// 服務器
http.Server(function(req,res) {
// 打亂200 個請求中的1 個
if (Math.floor(Math.random() * 200) === 4) {
console.log('Stopped ' + process.pid + ' from ever finishing')
while(true) { continue }
}
res.writeHead(200);
res.end('hello world from ' + process.pid + '\n')
}).listen(8000)
// 每秒鐘報告一次狀態(tài)
setInterval(function report(){
process.send({cmd: "reportMem", memory: process.memoryUsage(),
process: process.pid})
}, 1000)
}
function createWorker() {
var worker = cluster.fork()
console.log('Created worker: ' + worker.pid)
// 允許開機時間
workers[worker.pid] = {worker:worker, lastCb: new Date().getTime()-1000}
worker.on('message', function(m) {
if(m.cmd === "reportMem") {
workers[m.process].lastCb = new Date().getTime()
if(m.memory.rss > rssWarn) {
console.log('Worker ' + m.process + ' using too much memory.')
}
}
})
} 總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Node.js實現(xiàn)前端后端數(shù)據(jù)傳輸加密解密
這篇文章主要介紹了Node.js實現(xiàn)前端后端數(shù)據(jù)傳輸加密解密,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08
Node.js操作MySQL8.0數(shù)據(jù)庫無法連接的問題解決
使用node.js連接數(shù)據(jù)庫MySQL 8時候,顯示報錯 ER_NOT_SUPPORTED_AUTH_MODE,本文就來介紹一下解決方法,感興趣的可以了解一下2023-10-10
node.js中的querystring.unescape方法使用說明
這篇文章主要介紹了node.js中的querystring.unescape方法使用說明,本文介紹了querystring.unescape的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下2014-12-12

