An in-depth analysis of multi-threading and multi-processing in Node.js
Node.js is a free cross-platform JavaScript runtime environment. Although it is single-threaded in nature, it can use multiple threads in the background to Execute asynchronous code.
Due to the non-blocking nature of Node.js, different threads execute different callbacks, which are first delegated to the event loop. The Node.js runtime handles all of this. [Video tutorial recommendation: node js tutorial]
Why use NodeJS?
JavaScript was originally built as a single-threaded programming language that only runs in a web browser. This means that within a process, only one set of instructions can be executed at a given time.
Move to the next code block only after execution of the current code block is complete. However, the single-threaded nature of JavaScript makes implementation easy.
Initially, JavaScript was only used to add a small amount of interactivity to the website. So there is no need for multi-threading. But times have changed, users have become more demanding, and JavaScript has become “the most popular programming language on the Web.”
Multiple threads are becoming common nowadays. Since JavaScript is a single-threaded language, multi-threading cannot be implemented in it. Fortunately, in this case, there is a great solution: Node.js.
Node.js frameworks are not lacking, thanks to the general popularity of JavaScript runtime environments (especially JavaScript). Before continuing with this article, let us understand some important points about Node.js:
- You can use the send function to pass messages from child processes to other child processes and the main process
- Support Fork multiple processes
- No state is shared between the main process and the child process
Why fork the process?
In two cases we need to fork a process:
- Increase speed by delegating tasks to other processes
- For freeing memory and unloading a single Process
can send data to the child process and can also send it back.
The Node.js way
Node.js uses two types of threads:
- The main thread handles the
- work via the event loop There are many worker threads in the pool
The event loop is responsible for getting callbacks or functions and registering them for future execution. It runs in the same thread as the correct JavaScript code. Once a JavaScript operation blocks the thread, the event loop is also blocked.
The work pool is an execution model responsible for generating and processing different threads. It executes the task synchronously, then returns the result to the event loop, and finally the event loop provides the result to the callback.
In summary, the work pool is responsible for asynchronous I/O operations, that is, interaction with the system disk and network. Modules like fs and crypto are the main modules that use worker pools.
Since the worker pool is implemented in the libuv library, Node.js has a slight delay in internal communication between JS and C. But it's almost imperceptible.
Everything is fine until we encounter the requirement to perform complex operations synchronously. Any function that takes a large amount of time to execute will cause the main thread to block.
If a program has multiple CPU-intensive functions, it will cause a significant decrease in server throughput. In the worst case, the server will become unresponsive and unable to delegate tasks to the worker pool.
Domains such as AI, big data, and machine learning cannot benefit from Node.js because these operations block the main thread and make the server unresponsive. But that changes with the arrival of Node.js v10.5.0, which adds support for multi-threading.
Challenges of Concurrency and CPU-bound Tasks
Establishing concurrency in JavaScript can be difficult. Allowing multiple threads to access the same memory can lead to race conditions that not only make the failure difficult to reproduce, but also difficult to resolve.
Node.js was originally implemented as a server-side platform based on asynchronous I/O. This makes a lot of things easier by simply eliminating the need for threads. Yes, Node.js programs are single-threaded, but not in the typical way.
We can run in parallel in Node.js, but there is no need to create threads. The operating system and virtual machine work together to use I/O in parallel, and then when data needs to be sent back to the JavaScript code, the JS code runs in a single thread.
Everything except the JS code runs in parallel in Node.js. Unlike asynchronous blocks, synchronous blocks in JS are always executed once at a time. Waiting for I/O events to occur in JS takes much more time than executing code.
Node.js programs only call the required functions or callbacks without blocking the execution of other code. Initially neither JavaScript nor Node.js was intended to handle CPU-intensive or CPU-bound tasks.
When code is minimal, execution will be agile. But the greater the amount of calculation, the slower the execution speed.
If you still try to complete CPU-intensive tasks in JS and Node, it will freeze the UI in the browser and queue all I/O events. Still, we've come a long way. Now there is the worker_threads module.
The worker_threads module makes multithreading easy
Node.js v10.5.0 was released in June 2018, introducing the worker_threads module. It helps achieve concurrency in popular JavaScript runtime environments. This module allows the creation of fully functional multi-threaded Node.js applications.
Technically speaking, a worker thread is some code generated in a separate thread. To start using worker threads, you need to import the worker_threads module first. You then need to create an instance of the Worker class to create a worker thread.
When creating an instance of the Worker class, there are two parameters:
- The first parameter provides the file path with the extension .js or .mjs, which contains the code for the worker thread ,
- The second parameter provides an object containing the workerData property, which contains the data that will be accessed when the worker thread starts execution
The secondary thread is able to schedule multiple message events. Therefore, callback methods take precedence over returning promises.
Communication between worker threads is event-based, that is, the listener is set to be called immediately after the worker thread sends the event. The 4 most common events are:
worker.on('error', (error) => {});
- Emitted when there is an uncaught exception in the worker thread. Next the worker thread terminates and the error is available as the first argument in the callback.
worker.on('exit', (exitCode) => {})
- Emitted when the secondary thread exits. If
process.exit()
is called in a worker thread, the exitCode will be provided to the callback. Code 1 ifworker.terminate()
terminates the worker thread.
worker.on('message', (data) => {});
- Emitted when the worker thread sends data to the parent thread.
worker.on('online', () => {});
- Emitted when the worker thread stops parsing JS code and starts executing. Although not commonly used, the online event may provide more information in certain situations.
Ways of using worker threads
There are two ways to use worker threads:
- Method 1 – Involves generating work Thread that executes its code and sends the results to the parent thread. This method requires creating a new worker thread from scratch each time for a new task.
- Method 2 – Involves spawning a worker thread and setting up listeners for message events. Each time the message is triggered, the worker thread executes the code and sends the results back to the parent thread. The worker thread is kept alive for future use.
Method 2 is also known as worker pool. This is because the method involves creating a pool of workers, letting them wait, and dispatching message events to perform tasks when needed.
Since creating a worker thread from scratch requires creating a virtual machine and parsing and executing code, the official Node.js documentation recommends method 2. Additionally, Method 2 is more practical and more effective than Method 1.
Important properties available in the worker_threads module
- isMainThread – This property is true when not operating within a worker thread. If desired, you can include a simple if statement at the beginning of the worker file. This ensures it only runs as a worker thread.
- parentPort – An instance of MessagePort used to communicate with the parent thread.
- threadId – Unique identifier assigned to the worker thread.
- workerData – Data contained in the constructor of the worker thread.
Multiple processes in Node.js
In order for Node.js to take advantage of the capabilities of a multi-core system, some processes can be used. The popular JavaScript runtime environment has a module called cluster that provides support for multiple processes.
Use the cluster module to generate multiple child processes, and these child processes can share a common port. Systems using NodeJS can handle larger workloads when child processes are put into use.
Node.js on the backend
The Internet has become the platform of choice for millions of companies around the world. Therefore, in order for a business to reach its maximum potential, and stand out in the process, it is necessary to have a strong online presence.
It all starts with a powerful and intuitive website. To create a flawless website, it is important to choose the best front-end and back-end technologies. Although single-threaded in nature, Node.js is the first choice for developing backend web services.
Despite the plethora of backend multi-threading options, big-name companies still prefer Node.js. This is because Node.js provides workarounds for using multithreading in JavaScript, which is already "the most popular programming language on the web."
Summary
The worker_threads module provides an easy way to implement multi-threading in Node.js programs. By delegating heavy calculations to worker threads, you can significantly increase your server's throughput.
With support for multi-threading, Node.js will continue to attract an increasing number of developers, engineers and other professionals from compute-intensive fields such as AI, big data and machine learning.
English original address: https://flatlogic.com/blog/multi-threading-and-multiple-process-in-node-js/
To ensure readability Sex, this article adopts free translation rather than literal translation.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of An in-depth analysis of multi-threading and multi-processing in Node.js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor