search
HomeWeb Front-endJS TutorialHow to understand that Node.js is not a completely single-threaded program (a brief analysis)

Why do we say Node.js is not completely single-threaded? How to understand? The following article will discuss it with you, I hope it will be helpful to you!

How to understand that Node.js is not a completely single-threaded program (a brief analysis)

I believe everyone knows that node is a single-threaded program that uses Event Loop to achieve multiple concurrencies. Unfortunately this is not entirely correct.

So why is Node.js not a completely single-threaded program?

Node.js is a single-threaded program*

All the Javascript, V8, and event loops we wrote ourselves run in the same thread, which is the main thrad .

Hey, doesn’t this mean that node is single-threaded?

But maybe you don’t know that node has many modules with C code behind them.

Although node does not expose users to the permission to control threads, C can use multi-threading.

So when will node use multi-threading?

  • If a node method calls C's synchronous method behind the scenes, it will all run in the main thread.

  • If a node method calls C's asynchronous method behind the scenes, sometimes it does not run in the main thread.

Talk is cheap, show me the code.

Synchronous method, running in the main thread

Herecrypto Many related modules are written in C. The following program is a function for calculating hash, which is generally used to store passwords.

import { pbkdf2Sync } from "crypto";
const startTime = Date.now();
let index = 0;
for (index = 0; index < 3; index++) {
    pbkdf2Sync("secret", "salt", 100000, 64, "sha512");
    const endTime = Date.now();
    console.log(`${index} time, ${endTime - startTime}`);
}
const endTime = Date.now();
console.log(`in the end`);

Output time,

0 time, 44 
1 time, 90
2 time, 134
in the end

You can see that it takes about 45ms each time, and the code is executed sequentially on the main thread.

Pay attention to who is the final output? Note that a hash here takes ~45ms on my CPU.

Asynchronous pbkdf2 method does not run in the main thread

import { cpus } from "os";
import { pbkdf2 } from "crypto";
console.log(cpus().length);
let startTime = console.time("time-main-end");
for (let index = 0; index < 4; index++) {
    startTime = console.time(`time-${index}`);
    pbkdf2("secret", `salt${index}`, 100000, 64, "sha512", (err, derivedKey) => {
        if (err) throw err;
        console.timeEnd(`time-${index}`);
    });
}
console.timeEnd("time-main-end");

The output time,

time-main-end: 0.31ms
time-2: 45.646ms
time-0: 46.055ms
time-3: 46.846ms
time-1: 47.159ms

As you can see here, the main thread is early At the end, however, the time for each calculation is 45ms. You must know that the time it takes for a CPU to calculate hash is 45ms. The node here definitely uses multiple threads for hash calculation.

If I change the number of calls here to 10, then the time is as follows. You can see that as the number of CPU cores is used up, the time is also increasing. Once again, it is proved that node definitely uses multiple threads for hash calculation.

time-main-end: 0.451ms
time-1: 44.977ms
time-2: 46.069ms
time-3: 50.033ms
time-0: 51.381ms
time-5: 96.429ms // 注意这里,从第五次时间开始增加了
time-7: 101.61ms
time-4: 113.535ms
time-6: 121.429ms
time-9: 151.035ms
time-8: 152.585ms

Although it is proven here that node definitely has multi-threading enabled. But there is a little problem? The CPU of my computer is AMD R5-5600U, which has 6 cores and 12 threads. But why does the time increase from the fifth time? Node does not fully utilize my CPU?

what is the reason?

Node uses a predefined thread pool. The default size of this thread pool is 4.

export UV_THREADPOOL_SIZE=6

Let us take a look An example,

HTTP request

import { request } from "https";
const options = {
  hostname: "www.baidu.com",
  port: 443,
  path: "/img/PC_7ac6a6d319ba4ae29b38e5e4280e9122.png",
  method: "GET",
};

let startTime = console.time(`main`);

for (let index = 0; index < 15; index++) {
  startTime = console.time(`time-${index}`);
  const req = request(options, (res) => {
    console.log(`statusCode: ${res.statusCode}`);
    console.timeEnd(`time-${index}`);
    res.on("data", (d) => {
      // process.stdout.write(d);
    });
  });

  req.on("error", (error) => {
    console.error(error);
  });

  req.end();
}

console.timeEnd("main");
main: 13.927ms
time-2: 83.247ms
time-4: 89.641ms
time-3: 91.497ms
time-12: 91.661ms
time-5: 94.677ms
.....
time-8: 134.026ms
time-1: 143.906ms
time-13: 140.914ms
time-10: 144.088ms

The main program here also ended early. Here I started http request to download pictures 15 times, but the time they spent did not succeed. Multiplying, it seems not to be affected by the thread pool/cpu.

Why? ? Is Node using a thread pool?

If the asynchronous method of C behind Node will first try to see if there is kernel asynchronous support, for example, please use epoll (Linux) for the network here. If the kernel does not provide an asynchronous method, Node will Will use its own thread pool. .

So although the http request is asynchronous, it is implemented by the kernel. After the kernel is completed, C will be notified, and C will notify the main thread to handle the callback.

So which asynchronous methods of Node use the thread pool? Which ones won’t?

  • Native Kernal Async

    • TCP/UDP server client
    • Unix Domain Sockets (IPC)
    • pipes
    • dns.resolveXXX
    • tty input(stdin etc)
    • Unix signals
    • Child process
  • Thread pool

    • fs.*
    • dns.lookup
    • pipe (edge ​​case)

This It is also the entry point for most Node optimizations.

But how do these combine with the most important Event Loop?

Event Loop

I believe everyone is very familiar with Event loop. The event loop is like a distributor.

  • If it encounters an ordinary javascript program or callback, it is handed over to V8 for processing.

  • If you encounter a synchronization method written in C, hand it over to C and run it on the main thread.

  • If you encounter asynchronousThe back of the method is written in C. If there is kernel asynchronous support, it will be handed over from the main thread to the kernel for processing.

  • If it is asynchronousThe back of the method is written in C. If there is no kernel asynchronous support, it is handed over from the main thread to the thread pool.

  • Thread pool and the kernel will return the results to the event loop. If there is a javascript callback registered, it will be handed over to V8 for processing.

And then loop like this until there is nothing left to process.

So Node is not entirely a single-threaded program.

For more node-related knowledge, please visit: nodejs tutorial!

The above is the detailed content of How to understand that Node.js is not a completely single-threaded program (a brief analysis). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Vercel是什么?怎么部署Node服务?Vercel是什么?怎么部署Node服务?May 07, 2022 pm 09:34 PM

Vercel是什么?本篇文章带大家了解一下Vercel,并介绍一下在Vercel中部署 Node 服务的方法,希望对大家有所帮助!

node.js gm是什么node.js gm是什么Jul 12, 2022 pm 06:28 PM

gm是基于node.js的图片处理插件,它封装了图片处理工具GraphicsMagick(GM)和ImageMagick(IM),可使用spawn的方式调用。gm插件不是node默认安装的,需执行“npm install gm -S”进行安装才可使用。

怎么使用pkg将Node.js项目打包为可执行文件?怎么使用pkg将Node.js项目打包为可执行文件?Jul 26, 2022 pm 07:33 PM

如何用pkg打包nodejs可执行文件?下面本篇文章给大家介绍一下使用pkg将Node.js项目打包为可执行文件的方法,希望对大家有所帮助!

一文解析package.json和package-lock.json一文解析package.json和package-lock.jsonSep 01, 2022 pm 08:02 PM

本篇文章带大家详解package.json和package-lock.json文件,希望对大家有所帮助!

分享一个Nodejs web框架:Fastify分享一个Nodejs web框架:FastifyAug 04, 2022 pm 09:23 PM

本篇文章给大家分享一个Nodejs web框架:Fastify,简单介绍一下Fastify支持的特性、Fastify支持的插件以及Fastify的使用方法,希望对大家有所帮助!

node爬取数据实例:聊聊怎么抓取小说章节node爬取数据实例:聊聊怎么抓取小说章节May 02, 2022 am 10:00 AM

node怎么爬取数据?下面本篇文章给大家分享一个node爬虫实例,聊聊利用node抓取小说章节的方法,希望对大家有所帮助!

手把手带你使用Node.js和adb开发一个手机备份小工具手把手带你使用Node.js和adb开发一个手机备份小工具Apr 14, 2022 pm 09:06 PM

本篇文章给大家分享一个Node实战,介绍一下使用Node.js和adb怎么开发一个手机备份小工具,希望对大家有所帮助!

图文详解node.js如何构建web服务器图文详解node.js如何构建web服务器Aug 08, 2022 am 10:27 AM

先介绍node.js的安装,再介绍使用node.js构建一个简单的web服务器,最后通过一个简单的示例,演示网页与服务器之间的数据交互的实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools