node.js 集群可以在 64 位 Wintel PC 上产生多少个子进程? [英] How many child processes can a node.js cluster spawn on a 64bit Wintel PC?

查看:27
本文介绍了node.js 集群可以在 64 位 Wintel PC 上产生多少个子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行并发测试,为简洁起见,为每个欺骗性 http 请求定义了一个进程.它最多可用于 64 个请求/进程,但在 65 个时折叠.我在 I5 笔记本电脑上运行 Window 7(64 位),具有 4GB 内存.

I was running a concurrency test, and for brevity's sake, defined a process for each spoofed http request. It worked fine for up to 64 requests/processes, but folded on 65. I'm running Window 7 (64bit) on an I5 laptop, with 4GB of Ram.

在运行测试时,我打开了一个 Chrome(有几个选项卡),我希望操作系统的公共系统进程也会有一些影响,但我对最低级别的 node.js 知之甚少了解问题所在.

Whilst running the test I had a Chrome open (with a handful of tabs), and I expect that the OS' common system processes would also have some effect, but I know too little about node.js at the lowest level to understand where the problem lies.

例如,一篇文章建议在 2GB 64 位 Windows XP 系统上运行超过 8000 个进程是可能的:

For example, one article suggest it's possible to run well over 8000 processes on a 2GB 64-bit Windows XP system:

http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx

但是我遇到的64子进程图还是比较显眼的.

But the 64 child process figure that I ran into was rather conspicuous.

有什么想法吗?

推荐答案

节点是异步的,没有阻塞,只靠当前脚本,可以完美处理多个连接,也就是说在高并发的情况下,它会使用你所有的 CPU,但每个进程只能使用一个核心,因为 Node 没有线程.因此,从技术上讲,建议拥有与内核一样多的进程,每个进程一个内核.在这种情况下,在高并发情况下,Node 集群将使用所有 CPU.如果你做得更多,你就是在浪费你的 RAM 并在你的操作系统调度程序上投入额外的工作.除此之外,每个 nodejs 进程都有一个启动时间.所以在运行时创建一个nodejs进程是非常昂贵的.

Well node is asynchronous, there's no blocking, only by the current script and it can handle multiple connections perfectly, so that means on a high concurrency, it would use all of your CPU, but each process can only use one core, since Node is not threaded. So technically what is recommend to have is having as many processes as your cores, one core for each process. In that case, on a high concurrency the Node cluster would use all of the CPU. If you go more than that, you are wasting your RAM and putting extra work on your OS scheduler. Beside that each nodejs process have an startup time. So it is very expensive to create a nodejs process at run time.

来自 Node.JS 文档:

From Node.JS docs:

这些子节点仍然是 V8 的全新实例.假设至少每个新节点的启动时间为 30 毫秒,内存为 10 兆字节.也就是说,你不能创建成千上万个.

These child Nodes are still whole new instances of V8. Assume at least 30ms startup and 10mb memory for each new Node. That is, you cannot create many thousands of them.

结论 最好的做法是按照 CPU 内核的数量进行分叉,即:

Conclusion the best thing to do is to fork just as the number of your CPU cores, which is:

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker) {
    console.log('worker ' + worker.process.pid + ' died');
    cluster.fork();
  });
} else {
  // Worker processes have a http server.
  http.Server(function(req, res) {
    res.writeHead(200);
    res.end("hello world
");
  }).listen(8000);
}

我们实际上有一个生产服务器,这样做可能需要大约 1000 个并发,并且为 hello world 服务的延迟不到 10 毫秒.

We actually have a production server, doing that, which can take about 1000 concurrency, and less than 10ms latency serving the hello world.

这篇关于node.js 集群可以在 64 位 Wintel PC 上产生多少个子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆