Node.js进程能够使用超过100%的CPU吗?

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

Can a nodejs process use more than 100% CPU?

问题

我有一个运行任务的节点进程。当我运行一个密集的任务时,在top中可以显示超过100%的CPU使用率(大约110%)。从我正在进行的一些研究中,我发现node.js是单线程的,这意味着它只会在每个进程上运行在一个CPU上。

是否有可能工作负载可以占用整个CPU,以便将部分负载移至另一个CPU?我无法找到明确的答案。

英文:

I have a node process that runs tasks. When I run an intensive task, in top it can show more than 100% CPU usage (around 110%). From some research that I was doing, I figured that nodejs was single-threaded meaning it would only be running on one CPU per process.

Is it possible that the workload could take up the whole CPU so it moves some of the load to another CPU? Was unable to find a clear answer on this.

答案1

得分: 1

除了专门使用WorkerThreads进行编码(这似乎不是您正在使用的方式),Node.js只在单个线程中运行您的JavaScript代码(例如,解释器本身只使用一个线程来运行JavaScript操作码)。

但是,Node.js确实在库函数的实现中使用其他线程,例如文件系统操作和加密操作以及垃圾回收器。此外,一些第三方库可能会在其自己的实现中使用本机线程。因此,Node.js绝对可以使用多个核心,这取决于代码/任务的具体操作和调用了哪些库函数。

是否可能使工作负载占用整个CPU,以便将部分负载移到另一个CPU上?

它不会将您的JavaScript运行迁移到另一个CPU上。但正如我上面所说,一些使用本机代码的库函数可能会使用额外的线程。

英文:

Other than specifically coding with WorkerThreads (which it doesn't sound like you are using), nodejs runs your Javascript code in only a single thread (e.g. the interpreter itself just uses one thread to run Javascript opcodes).

But, nodejs does have other threads that are used in the implementation of library functions such as file system operations and crypto operations and for the garbage collector. And, some 3rd party libraries may use native threads in their own implementation. So, it is definitely possible for nodejs to use more than just one core. It really depends upon what the code/task is doing and what library functions are being called.

> Is it possible that the workload could take up the whole CPU so it moves some of the load to another CPU?

It does not move the running of your Javascript to another CPU. But as I said above, some library functions that use native code may use additional threads.

huangapple
  • 本文由 发表于 2023年1月5日 02:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75009933.html
匿名

发表评论

匿名网友

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

确定