Using multi-processor for node
来源:互联网 发布:优化营商环境宣传口号 编辑:程序博客网 时间:2024/05/18 13:11
Node is single threaded. This means Node is only using one processor to do its work. However, most servers have several "multi-core" processors, and you can a single multi-core processor has many processors. A server with two physical CPU sockets might have "24 logical cores," that is 24 processors exposed to the operating system. In order to make the best use of Node we should use those too. So if we don't have threads, how do we do that?
Node provides a module called cluster
that allows you to deligate work to child processes. This means that Node creates a copy of its current program in another process (on Windows, it is actually another thread). Each child process has some special abilities, such as the ability to share a socket with other children. This allows us to write Node programs that start many other Node programs and then delegate work to them.
It is important to understand that when you use cluster
to share work between a number of copies of a Node program, the master process isn't involved in every transaction. The master process manages the child processes, but when the children interact with I/O they do it directly, not through the master. This means if you set up a web server using cluster
, requests don't go through your master process, but directly to the children. Hence, dispatching requests does not create a bottleneck in the system.
/* running node on multi-processers */
var cluster = require('cluster'),
http = require('http'),
NR_CPUS = require('os').cpus().length,
numReqs = {},
totle =0; /* account for amount requests */
if (cluster.isMaster) {
console.log('INFO: there are ' + NR_CPUS + ' cpus here');
for (var i = 0; i < NR_CPUS; i += 1 ) {
var worker = cluster.fork();
numReqs[Number(worker.pid).toString()] =0;
worker.on('message',function(msg) {
if ( msg.cmd&& 'notifyRequest' ===msg.cmd ) {
console.log('\n');
numReqs[msg.pid] +=1;
totle +=1;
for ( var pid in numReqs ) {
if ( undefined !== numReqs[pid] ) {
console.log('worker PID[' + pid + '] gets ' + numReqs[pid] + ' requests now');
}
}
console.log('total requests are ' + totle);
}
});
}
cluster.on('death',function(worker) {
console.log('[worker ' + worker.pid + ' died]');
/* remove the request counter of dead process */
numReqs[worker.pid] = undefined;
/* fork another worker and add its request counter */
var rebirth = cluster.fork();
numReqs[Number(rebirth.pid).toString()] =0;
console.log('[worker ' + rebirth.pid +' join us now]');
/* add the message event listener on that new worker */
rebirth.on('message',function(msg) {
if ( msg.cmd && 'notifyRequest' === msg.cmd ) {
console.log('\n');
numReqs[msg.pid] +=1;
total +=1;
for ( var pid in numReqs ) {
if ( undefined !== numReqs[pid] ) {
console.log('worker PID[' + pid + '] gets ' + numReqs[pid] + ' requests now');
}
}
console.log('total requests are ' + total);
}
});
});
} else {
/* Child processes serving on http protocal */
http.createServer(function(req, res) {
/* send msg to master process */
process.send({
cmd:'notifyRequest',
pid: process.pid.toString()
});
res.writeHead(200, {
'Content-Type':'text/plain'
});
res.end('worker PID[' + process.pid + '] start serving you now:)\n');
}).listen(8888);
- Using multi-processor for node
- Intel Threading Building Blocks: Outfitting C++ for Multi-core Processor Parallelism [ILLUSTRATED]
- Cache Cohernce with Multi-Processor
- Using Node.js for Javascript Analysis
- Hbase Table Isolation for Multi-tenancy using Region Server Grouping
- Using AfxBeginThread for multi-thread in VS C++
- Address Disposition for Processor
- coder-strike 2014 B Multi-core Processor
- smp-symmetric multi-processor 对称多处理器
- CodeForces 411B Multi-core Processor
- Using Multi Tabled Datasets
- Using Multi-Threaded Tests
- Software Development for Embedded Multi-core Systems: A Practical Guide Using Embedded Intel® Ar
- 多任务学习“Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics”
- Does Android Support Dual-Cores or Multi-Cores Processor
- Coder-Strike 2014 - Qualification Round B. Multi-core Processor
- multi-level sort using Python
- [openstack swift]install multi node
- Oracle触发器简单示例
- 再谈 .net dll泛滥问题,项目引用的管理问题 .完美方案
- 字符串左旋满足时间复杂度为O(n)
- MKey3G培训总结
- VC中的0XCCCCCCCC
- Using multi-processor for node
- U-BOOT启动过程分析 经典文章汇集
- activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver解释
- 最小生成树 (Kruscal 、prime)
- 计算机方面经典书籍<一>
- nginx实现网站负载均衡(windows+IIS负载实测)
- Formality简单实用之1--RTLvsRTL
- IOS 下的 HelloWorld程序 续 事件处理
- 为什么INT_MIN不是直接写成-2147483648