Table of Contents
Process
Thread
#Processes in node
child_process module
cluster module
Inter-process communication
Home Web Front-end JS Tutorial Is Node.js really single-threaded? How to communicate between processes?

Is Node.js really single-threaded? How to communicate between processes?

Aug 02, 2021 pm 06:44 PM
node.js single thread

This article will introduce processes and threads to you, take you to understand the threads in Node.js, see if node is really single-threaded, child_process and cluster modules, and briefly talk about more How threads communicate with each other.

Is Node.js really single-threaded? How to communicate between processes?

Process

In the operating system, the explanation of process: A process is a program with certain independent functions in a data A dynamic execution process on the set is an independent unit for resource allocation and scheduling by the operating system, and is the carrier for application running.

  • is a running process of the application (dynamic concept)

  • is the basic unit for system allocation and scheduling of resources (a process is generally composed of a program , data collection and process control block)

  • Each process has its own independent space address and data stack (data is not shared between processes and can be communicated through other methods) A process generally has five states: initial state, execution state, waiting state, ready state, and termination state.

Thread

Thread is when the program is executing A single sequential control process is the smallest unit of program execution flow and the basic unit of processor scheduling and dispatch.

  • The smallest unit of task scheduling and execution

  • A single execution route of code in a process

The difference between process and thread

Thread is the smallest unit of program execution, and process is the smallest unit of resources allocated by the operating system. A process consists of one or more threads, and a thread is the code in a process. Processes of different execution routes are independent of each other, but the memory space of the program is shared between threads in the same process. Thread context switching is faster than process context switching.

#Processes in node

node is single-threaded, that is, a process only opens one thread node. [Recommended study: "nodejs Tutorial"]

Is it really single-threaded?

Although Node is single-threaded, its underlying layer is multi-threaded. In the event loop, the libuv library takes out tasks from the event queue and assigns them to different threads for processing. Now the hardware conditions are not as backward as before. If only a single thread is used for operation, resources will be wasted. Therefore, in order to achieve multi-process processing and give full play to the advantages of multi-core CPUs, Node provides the child_process module and cluster module.

  • The child_process module is used to start multiple child processes, and run different commands or execute node.js module files and executable files in the child processes

  • Cluster module, cluster module, is used to start multiple child processes in a Node.js application and run a copy of the Node.js application in each child process

child_process module

  • child_process.spawn(): Suitable for returning large amounts of data, such as image processing and binary data processing.

  • child_process.exec(): Suitable for small amounts of data. The default value of maxBuffer is 200 * 1024. Exceeding this default value will cause the program to crash. If the amount of data is too large, spawn can be used.

  • child_process.execFile(): similar to child_process.exec(), the difference is that it cannot be executed through the shell and does not support behaviors like I/O redirection and file search

  • child_process.fork(): Spawn a new process. The processes are independent of each other. Each process has its own V8 instance and memory. System resources are limited. It is not recommended to spawn too many processes. The number of child processes coming out is usually set according to the number of system * CPU cores.

cluster module

  • cluster.fork([env]) Start the child process and create a Node in the child process Example of .js application

  • isMaster attribute and isWorker attribute are used to determine whether it is running in the main process or in a child process

  • Workers attribute is used to obtain worker objects running in all sub-processes

Expanding, how do multiple threads communicate?

Inter-process communication

Four types:

  • Message passing (pipeline, FIFO, message queue)

  • Semaphore (mutex, condition variable, read-write lock)

  • Shared memory (anonymous, named)

  • Remote Procedure Call

Inter-process communication is not only encountered in node, but also in other languages. Of course, it is also a must-ask question in the interview. .

Many contents can be discussed in depth!

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Is Node.js really single-threaded? How to communicate between processes?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

See all articles