简体   繁体   English

node.js 集群可以在 64 位 Wintel PC 上产生多少个子进程?

[英]How many child processes can a node.js cluster spawn on a 64bit Wintel PC?

I was running a concurrency test, and for brevity's sake, defined a process for each spoofed http request.我正在运行并发测试,为简洁起见,为每个欺骗性 http 请求定义了一个进程。 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.它适用于多达 64 个请求/进程,但在 65 个时折叠。我在带有 4GB 内存的 I5 笔记本电脑上运行 Window 7(64 位)。

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.在运行测试时,我打开了一个 Chrome(有几个选项卡),我希望操作系统的公共系统进程也会有一些影响,但是我对最低级别的 node.js 知之甚少,无法理解在哪里问题在于。

For example, one article suggest it's possible to run well over 8000 processes on a 2GB 64-bit Windows XP system:例如,一篇文章建议在 2GB 64 位 Windows XP 系统上运行超过 8000 个进程是可能的:

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

But the 64 child process figure that I ran into was rather conspicuous.但是我遇到的64个子进程图比较显眼。

Any ideas?有任何想法吗?

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.那么node是异步的,没有阻塞,只靠当前脚本,它可以完美处理多个连接,所以这意味着在高并发下,它会使用你所有的CPU,但每个进程只能使用一个核心,因为Node是没有螺纹。 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.在这种情况下,在高并发情况下,Node 集群将使用所有 CPU。 If you go more than that, you are wasting your RAM and putting extra work on your OS scheduler.如果你做的更多,你就是在浪费你的 RAM 并在你的操作系统调度程序上做额外的工作。 Beside that each nodejs process have an startup time.除此之外,每个 nodejs 进程都有一个启动时间。 So it is very expensive to create a nodejs process at run time.所以在运行时创建一个 nodejs 进程是非常昂贵的。

From Node.JS docs:来自 Node.JS 文档:

These child Nodes are still whole new instances of V8.这些子节点仍然是 V8 的全新实例。 Assume at least 30ms startup and 10mb memory for each new Node.假设每个新节点至少需要 30 毫秒的启动时间和 10 兆的内存。 That is, you cannot create many thousands of them.也就是说,您无法创建成千上万个。

Conclusion the best thing to do is to fork just as the number of your CPU cores, which is:结论 最好的做法是按照 CPU 内核的数量进行分叉,即:

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\n");
  }).listen(8000);
}

We actually have a production server, doing that, which can take about 1000 concurrency, and less than 10ms latency serving the hello world.我们实际上有一个生产服务器,这样做可能需要大约 1000 个并发,并且为 hello world 服务的延迟不到 10 毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM