Table of Contents
Why use NodeJS?
Why fork the process?
The Node.js way
Challenges of Concurrency and CPU-bound Tasks
The worker_threads module makes multithreading easy
Ways of using worker threads
Important properties available in the worker_threads module
Multiple processes in Node.js
Node.js on the backend
Summary
Home Web Front-end JS Tutorial An in-depth analysis of multi-threading and multi-processing in Node.js

An in-depth analysis of multi-threading and multi-processing in Node.js

Aug 31, 2020 am 09:55 AM
node.js stream flow

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:

  1. You can use the send function to pass messages from child processes to other child processes and the main process
  2. Support Fork multiple processes
  3. 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:

  1. Increase speed by delegating tasks to other processes
  2. 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:

  1. The main thread handles the
  2. 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:

  1. The first parameter provides the file path with the extension .js or .mjs, which contains the code for the worker thread ,
  2. 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) => {});
Copy after login
  1. 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) => {})
Copy after login
  1. 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 if worker.terminate() terminates the worker thread.
worker.on('message', (data) => {});
Copy after login
  1. Emitted when the worker thread sends data to the parent thread.
worker.on('online', () => {});
Copy after login
  1. 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!

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

Video Face Swap

Video Face Swap

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

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)

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!

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

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.

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!

How to debug Java Stream operations in IntelliJ IDEA How to debug Java Stream operations in IntelliJ IDEA May 09, 2023 am 11:25 AM

Stream operation is a highlight of Java8! Although java.util.stream is very powerful, there are still many developers who rarely use it in actual work. One of the most complained reasons is that it is difficult to debug. This was indeed the case at the beginning, because streaming operations such as stream cannot be used in DEBUG When it is one line of code, when it comes to the next step, many operations are actually passed at once, so it is difficult for us to judge which line in it is the problem. Plug-in: JavaStreamDebugger If the IDEA version you are using is relatively new, this plug-in is already included and does not need to be installed. If it is not installed yet, install it manually and then continue below.

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".

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

How to get max value from stream in java8 How to get max value from stream in java8 May 14, 2023 pm 03:43 PM

java8's stream takes maxpublicstaticvoidmain(String[]args){Listlist=Arrays.asList(1,2,3,4,5,6);Integermax=list.stream().max((a,b)->{if (a>b){return1;}elsereturn-1;}).get();System.out.println(max);}Note: The size is determined here through positive and negative numbers and 0 values. Instead of writing it directly if(a>b){returna;}elseretur

See all articles