这个单一的 Node.JS 应用程序使用多核吗? [英] Is This Single Node.JS App Using Multiple Cores?

查看:31
本文介绍了这个单一的 Node.JS 应用程序使用多核吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在仅运行 1 个摄取数据馈送的 Node.js 脚本的 Ubuntu 18.04 系统上,htop 显示正在使用两个 CPU 内核.

On an Ubuntu 18.04 system running only 1 Node.js script that ingests a data feed, htop shows that both CPU cores are being utilized.

运行 1 个节点脚本:

没有运行 node.js 脚本:

这个 Node.js 脚本有多个事件侦听器,用于接收数据、进行一些数据处理并将它们发送到远程数据库服务器.

This Node.js script has multiple event listeners that receives data, does some data processing and sends them to a remote database server.

foo.on('msg', msg => { setImmediate(() => do_work(msg)) } );

问题:尽管 Node.js 是单线程的,但为什么看起来两个 CPU 内核被 Node 使用得相当平等?观察到的 CPU 负载分配为 60%/40% 和 50%/50%

Question: Why does it appear as if both CPU cores are being utilized rather equally by Node, despite Node.js being single threaded? CPU load splits observed are 60%/40% and 50%/50%

它实际上使用了两个 CPU 内核吗?或者只是一直在它们之间快速切换,但在任何时候都只真正使用 1 个内核?换句话说,这种情况会导致系统在 CPU 工作负载高于 1 核但低于 2 核时卡住.

Is it actually utilizing both CPU cores? Or simply switching between them really quickly all the time, but only really utilizing 1 core at any one time? In other words, such a scenario will cause the system to choke when the CPU workload is above 1 core's worth but lesser than 2 core's.

基本上,我想知道单核系统是否足以应对这种工作负载.谢谢!

Basically, I like to know whether a single-core system will suffice for this work load. Thank you!

推荐答案

即使只有一个 JS 函数(每个环境)1 将在任何给定时间执行,在一个内核上,这不会'意思是

Even though only a single JS function (per environment)1 will execute at any given time, on one core, that doesn't mean

  • that it always is the same core - the OS can move the thread at will (see Why does a single threaded process execute on several processors/cores?, Single-threaded application being balanced across CPU cores?, Why is a single thread spread across CPU's?, or How does a single thread run on multiple cores?)
  • that the node.js runtime itself is singlethreaded. It might do processing of asynchronous work in background threads, it might run garbage collection (see Can garbage collection happen while the main thread is busy? or Garbage Collector and concurrent marking in V8), and more.

1:你提到你只运行一个脚本,这意味着只有一个 JS 对象领域.node.js 应用程序在多个线程上运行的另一种可能性是产生 workers,每个有自己的JS环境,通过事件进行通信.

1: you mention that you're running only a single script, which means there is only one realm of JS objects. Another possibility for a node.js app to run on multiple threads is to spawn workers, each of which has its own JS environment and communicates via events.

这篇关于这个单一的 Node.JS 应用程序使用多核吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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