如何修复 NodeJS 未充分利用 CPU 核心的问题?

huangapple go评论77阅读模式
英文:

How to fix NodeJS underutilizing CPU Cores?

问题

根据这个页面Go vs Node.js,当运行CPU密集型代码时,Node.js似乎没有充分利用CPU核心。

如果我使用虚拟化并简单地添加更多的Node.js实例,我能够达到与Go相同的性能吗?我想可能仍然会有开销,并且可能无法达到相同的性能水平。

英文:

According to this page Go vs Node.js, Node.js is not showing to be taking full advantage of CPU cores when running cpu-intensive code.

If I use virtualization and simply add more Node.js instances, will I achieve the same performance as Go? I suppose there still will be overheads and one won't be able to achieve the same performance.

答案1

得分: 2

多个进程可以实现。对于4个CPU/线程,您需要4个Node.js进程来利用它们。不过,这需要一个可以在进程之间分割工作负载的工作负载。

Node.js提供了Cluster模块,可以将套接字连接分发给多个工作进程,这在某些工作负载下可能有所帮助,但我怀疑这对于任何基准工作负载都没有帮助。

英文:

Multiple processes will do. For 4 cpus/threads you need 4 Node.js processes to make use of them. That requires a workload that can be split between processes though.

Node.js provides the Cluster module to distribute socket connections between multiple worker processes which may help in some workloads, but I doubt this would help any of the benchmark workloads.

答案2

得分: 0

你需要安装Node.js的Cluster模块,以便在运行CPU密集型代码时充分利用CPU核心。

英文:

You need to install Cluster module of nodejs in order to take full advantage of CPU cores when running cpu-intensive code.

答案3

得分: 0

Node.js确实是一个单线程的进程。然而,你可以利用clustering在多个核心上生成多个工作进程。

如果你没有使用PM2来生成应用程序,请考虑使用它。PM2可以轻松地在多个核心上生成工作进程。

> pm2 start app.js -i max

这个命令将在每个可用的核心上生成工作进程。

另外,请注意,如果你正在使用会话/套接字,可能会因为集群而遇到一些问题。

英文:

Node.js is indeed a single threaded process. However, you can make use of clustering to spawn multiple workers on multiple cores.

If you are not using PM2 for spawning the app, please consider using it. PM2 makes spawning workers on multiple cores super easy.

> pm2 start app.js -i max

This command will spawn workers on each available core.

Also, note that if you are using session/sockets then you might face some problems because of clustering.

huangapple
  • 本文由 发表于 2017年3月29日 15:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/43087315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定