多核机器上的Node.js [英] Node.js on multi-core machines

查看:122
本文介绍了多核机器上的Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js 看起来很有趣,但是我必须错过了一些东西 - Node.js是不是只调整运行在一个进程和线程上?

Node.js looks interesting, BUT I must miss something - isn't Node.js tuned only to run on a single process and thread?

那么它如何扩展到多核CPU和多CPU服务器?毕竟,尽可能快速地制作单线程服务器,但是对于高负载我想要使用多个CPU。同样可以使应用程序更快 - 似乎今天的方式是使用多个CPU并并行化任务。

Then how does it scale for multi-core CPUs and multi-CPU servers? After all, it is all great to make fast as possible single-thread server, but for high loads I would want to use several CPUs. And the same goes for making applications faster - seems today the way is use multiple CPUs and parallelize the tasks.

Node.js如何适应这张图片?它的想法是以某种方式分发多个实例或什么?

How does Node.js fit into this picture? Is its idea to somehow distribute multiple instances or what?

推荐答案

[此帖子是最新的2012-09-02(比上面更新)。]



Node.js绝对可以在多核机器上进行扩展。

[This post is up-to-date as of 2012-09-02 (newer than above).]

Node.js absolutely does scale on multi-core machines.

是的,Node.js是每个进程一个线程。这是一个非常慎重的设计决策,无需处理锁定语义。如果您不同意这一点,您可能还没有意识到调试多线程代码是多么的难以理解。有关Node.js流程模型的更深入解释以及为什么它以这种方式工作(以及为什么它永远不会支持多个线程),请阅读我的其他帖子

Yes, Node.js is one-thread-per-process. This is a very deliberate design decision and eliminates the need to deal with locking semantics. If you don't agree with this, you probably don't yet realize just how insanely hard it is to debug multi-threaded code. For a deeper explanation of the Node.js process model and why it works this way (and why it will NEVER support multiple threads), read my other post.

两种方式:


  • 对于像图像编码这样的重型计算任务,Node.js可以激活孩子处理或向其他工作进程发送消息。在这个设计中,你有一个线程来管理事件流和N个进程执行繁重的计算任务并咀嚼其他15个CPU。

  • 为了扩展web服务的吞吐量,你应该在一个盒子上运行多个Node.js服务器,每个核心一个,并在它们之间分割请求流量。这提供了出色的CPU亲和力,并且随着核心数量几乎线性地扩展吞吐量。

由于v6.0.X Node.js已包含群集模块直接开箱即用,这样可以轻松设置可以侦听单个端口的多个节点工作程序。请注意,这与通过 npm 。

Since v6.0.X Node.js has included the cluster module straight out of the box, which makes it easy to set up multiple node workers that can listen on a single port. Note that this is NOT the same as the older learnboost "cluster" module available through npm.

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  http.Server(function(req, res) { ... }).listen(8000);
}

工人将竞争接受新连接,并且最不可能加载最少的进程赢了。它运行良好,可以在多核盒上很好地扩大吞吐量。

Workers will compete to accept new connections, and the least loaded process is most likely to win. It works pretty well and can scale up throughput quite well on a multi-core box.

如果你有足够的负载来关心多核,那么你将需要还要做更多的事情:

If you have enough load to care about multiple cores, then you are going to want to do a few more things too:


  1. 在像 Nginx Apache - 可以进行连接限制的东西(除非您希望过载条件完全打开盒子),重写URL,提供静态内容以及代理其他子服务。

  1. Run your Node.js service behind a web-proxy like Nginx or Apache - something that can do connection throttling (unless you want overload conditions to bring the box down completely), rewrite URLs, serve static content, and proxy other sub-services.

定期回收您的工作进程。对于长时间运行的过程,即使很小的内存泄漏也会最终累加。

Periodically recycle your worker processes. For a long-running process, even a small memory leak will eventually add up.

设置日志收集/监控






PS:Aaron和Christopher在另一篇文章的评论中进行了讨论(撰写本文时,其中最高职位)。对此有一些评论:


PS: There's a discussion between Aaron and Christopher in the comments of another post (as of this writing, its the top post). A few comments on that:


  • 共享套接字模型非常方便允许多个进程侦听单个端口并竞争接受新端口连接。从概念上讲,你可以想到preforked Apache这样做的重要警告是每个进程只接受一个连接然后死掉。 Apache的效率损失是分支新进程的开销,与套接字操作无关。

  • 对于Node.js,让N个工作者在单个套接字上竞争是非常的合理解决。另一种方法是建立一个像Nginx这样的机上前端,并为每个工作人员提供代理流量,在工作人员之间交替分配新连接。这两种解决方案具有非常相似的性能特征因为,正如我上面提到的,你可能想要让Nginx(或替代方案)面向你的节点服务,这里的选择实际上是:

共享端口: nginx(端口80) - > Node_workers x N(共享端口3000 w / Cluster)

vs

个别端口: nginx(端口80) - > {Node_worker(端口3000),Node_worker(端口3001) ),Node_worker(端口3002),Node_worker(端口3003)...}

单个端口设置可以带来一些好处(在进程之间具有较少耦合的潜力,具有更复杂的负载平衡决策等等),但设置肯定是更多工作,内置集群模块是一种低复杂性的替代方案,适用于大多数人。

There are arguably some benefits to the individual ports setup (potential to have less coupling between processes, have more sophisticated load-balancing decisions, etc.), but it is definitely more work to set up and the built-in cluster module is a low-complexity alternative that works for most people.

这篇关于多核机器上的Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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