简体   繁体   English

NodeJS中的Workers将产生的线程数

[英]Number of threads Workers in NodeJS will spawn

I am trying to understand the working of workers in NodeJS.我试图了解 NodeJS 中工作人员的工作。 My understanding is everytime we spawn a worker , it will create a new thread with it own Node/V8 instance.我的理解是每次我们生成一个 worker 时,它都会创建一个拥有自己的 Node/V8 实例的新线程。

So will the below code spawn 50 threads?那么下面的代码会产生 50 个线程吗?

How is it distributed over the cpu cores?它是如何分布在 CPU 内核上的?

This is the index.js这是 index.js

const { Worker } = require("worker_threads");
var count = 0;

console.log("Start Program");

const runService = () => {
  return new Promise((resolve, reject) => {
    const worker = new Worker("./service.js", {});
    worker.on("message", resolve);
    worker.on("error", reject);
    worker.on("exit", code => {
      if (code != 0) {
        reject(new Error("Worker has stopped"));
      }
    });
  });
};

const run = async () => {
  const result = await runService();
  console.log(count++);
  console.log(result);
};

for (let i = 0; i < 50; i++) {
  run().catch(error => console.log(error));
}

setTimeout(() => console.log("End Program"), 2000);

and this is the service.js file这是 service.js 文件

const { workerData, parentPort } = require("worker_threads");

// You can do any heavy stuff here, in a synchronous way
// without blocking the "main thread"
const sleep = () => {
  return new Promise(resolve => setTimeout(() => resolve, 500));
};
let cnt = 0;
for (let i = 0; i < 10e8; i += 1) {
  cnt += 1;
}
parentPort.postMessage({ data: cnt });

So will the below code spawn 50 threads?那么下面的代码会产生 50 个线程吗?

 .... for (let i = 0; i < 50; i++) { run().catch(error => console.log(error)); }

Yes.是的。

How is it distributed over the cpu cores?它是如何分布在 CPU 内核上的?

The OS will handle this.操作系统将处理此问题。

Depending on the OS, there is a feature called processor affinity that allow you to manually set the "affinity" or preference a task has for a CPU core.根据操作系统,有一个称为处理器关联的功能,允许您手动设置任务对 CPU 内核的“关联”或首选项。 On many OSes this is just a hint and the OS will override your preference if it needs to.在许多操作系统上,这只是一个提示,如果需要,操作系统将覆盖您的偏好。 Some real-time OSes treat this as mandatory allowing you more control over the hardware (when writing algorithms for self-driving cars or factory robots you sometimes don't want the OS to take control of your carefully crafted software at random times).一些实时操作系统将此视为强制性的,允许您更好地控制硬件(在为自动驾驶汽车或工厂机器人编写算法时,您有时不希望操作系统随机控制您精心制作的软件)。

Some OSes like Linux allow you to set processor affinity with command line commands so you can easily write a shell script or use child_process to fine-tune your threads.某些操作系统(如 Linux)允许您使用命令行命令设置处理器关联,以便您可以轻松编写 shell 脚本或使用child_process来微调您的线程。 At the moment there is no built-in way to manage processor affinity for worker threads.目前没有内置的方法来管理工作线程的处理器亲和性。 There is a third party module that I'm aware of that does this on Windows and Linux: nodeaffinity but it doesn't work on Max OSX (and other OSes like BSD, Solaris/Illumos etc.).我知道有一个第三方模块可以在 Windows 和 Linux 上执行此操作: nodeaffinity但它不适用于 Max OSX(以及其他操作系统,如 BSD、Solaris/Illumos 等)。

请参阅尝试了解此 Nodejs 是单线程的,并且在启动时它使用线程,因此工作线程的数量取决于您的系统创建的线程数量,并尝试分叉子进程更多线程的数量无济于事,它只会减慢整个进程过程并导致生产力下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM