本文介绍了节点:一核多进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上查了一下,似乎找到了与Node 如何从在多核 cpu 中运行中受益?"这个问题相关的答案.

I have looked up online and all I seem to find are answers related to the question of "how does Node benefit from running in a multi core cpu?"

但是.如果您的机器只有一个内核,则在任何给定时间您只能运行一个进程.(我正在考虑这里的任务调度).并且节点使用单线程模型.

But. If you have a machine with just one core, you can only be running one process at any given time. (I am considering task scheduling here). And node uses a single threaded model.

我的问题:是否有在一个内核中运行多个节点进程有意义的场景?如果该进程是一个侦听端口的 Web 服务器,那么鉴于只有一个进程可以侦听,这如何工作?

My question: is there any scenario in which it makes sense to run multiple node processes in one core? And if the process is a web server that listens on a port, how can this ever work given that only one process can listen?

推荐答案

是的,有一些场景.请参阅下面的详细信息.

Yes, there are some scenarios. See details below.

如果进程是一个监听端口的网络服务器,那怎么办?鉴于只有一个进程可以监听,这是否可行?

node.js 集群模块创建了一个场景,其中在所需端口上有一个侦听器,但传入请求在所有集群进程之间共享.更多详情请关注...

The node.js clustering module creates a scenario where there is one listener on the desired port, but incoming requests are shared among all the clustered processes. More details to follow...

您可以使用集群模块运行多个进程,这些进程都配置为处理同一端口上的传入请求.如果你想知道传入的请求是如何在不同的集群进程之间共享的,你可以阅读 这篇博文.简而言之,最终在所需端口上只有一个侦听器,并且传入请求在各种集群进程之间共享.

You can use the clustering module to run more than one process that are all configured to handle incoming requests on the same port. If you want to know how incoming requests are shared among the different clustered processes, you can read the explanation in this blog post. In a nutshell, there ends up being only one listener on the desired port and the incoming requests are shared among the various clustered processes.

至于您是否可以从比核心数更多的流程中受益,答案是这取决于您正在寻找哪种类型的收益.如果您有一个正确编写的带有所有异步 I/O 的服务器,那么添加比内核更多的进程可能不会提高您的整体吞吐量(以您的服务器可以处理的每秒请求数来衡量).

As to whether you could benefit from more processes than you have cores, the answer is that it depends on what type of benefit you are looking for. If you have a properly written server with all async I/O, then adding more processes than cores will likely not improve your overall throughput (as measured by requests per second that your server could process).

但是,如果您的请求中有任何占用大量 CPU 的处理,那么多一些进程可能会在同时请求之间提供更公平的调度,因为操作系统将在您的每个进程之间共享"CPU.这可能会略微降低整体吞吐量(因为在进程之间切换 CPU 的任务会增加开销),但即使有多个请求要一起处理,它也可能使请求处理更多.

But, if you have any CPU-heavy processing in your requests, having a few more processes may provide a bit fairer scheduling between simultaneous requests because the OS will "share" the CPU among each of your processes. This will likely slightly decrease overall throughput (because of the added overhead of task switching the CPU between processes), but it may make request processing more even when there are multiple requests to be processed together.

如果您的请求没有大量占用 CPU 的处理并且大部分时间实际上只是在等待 I/O,那么添加比内核更多的进程可能没有任何好处.

If your requests don't have much CPU-heavy processing and are really just waiting for I/O most of the time, then there's probably no benefit to adding more processes than cores.

因此,这实际上取决于您要针对什么进行优化以及您的情况.

So, it really depends what you want to optimize for and what your situation is.

这篇关于节点:一核多进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:13