Home Web Front-end JS Tutorial Summary of non-blocking IO and event loop in Node.js_node.js

Summary of non-blocking IO and event loop in Node.js_node.js

May 16, 2016 pm 04:36 PM
node.js event loop non-blocking io

It has been two months since I learned and used Node.js. I used express combined with mongoose to write a web application and a set of RESTful web api. Looking back at the introduction to Node.js on the homepage of the Node.js official website: Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. So what does the non-blocking I/O model mean?

Non-blocking IO model

First of all, IO operations are undoubtedly time-consuming. When the server receives a large number of requests, creating a process or thread for each request also adds additional memory overhead and may waste more time resources.

Since Node.js is event-driven, it uses an event loop to solve the bottleneck problem caused by IO operations. In Node.js, an IO operation usually has a callback function. When the IO operation is completed and returns, this callback function will be called, and the main thread will continue to execute the next code. Let’s simply use an example to illustrate this problem:

request('http://www.google.com', function(error, response, body) {
   console.log(body);
});
 
console.log('Done!');
Copy after login

The meaning of this code is to make a request to 'http://www.google.com'. When the request returns, the callback function is called to output the response information. Due to the operating mechanism of Node.js, after this code is run, 'Done!' will be output to the console immediately, and then the response information will be output after a period of time.

Event loop event loop

Next, let’s discuss the mechanism of the event loop. First, let’s talk about calling 桟. For example, there is the following piece of code:

function A(arg, func){
  var a = arg;
 
  func();
  console.log('A');  
}
 
function B(){
  console.log('B');
}
 
A(0, B);
Copy after login

When the code is executed, function A is first pushed into the calling bucket to become the top element of the stack and starts executing A. During the execution process, function B is pushed into the calling bucket to become the top element of the stack. After the execution of B is completed, B After being popped out of the calling stack, A becomes the top element of the stack again. After the execution of A is completed, A is popped out of the calling stack, and the calling stack becomes idle.

There is a message queue in the Javascript runtime, and the message is associated with a callback function. When an event is triggered, if the event has a corresponding callback function, the message will be added to the message queue. .

Looking back at what exactly the event loop loops about, after the code starts executing, functions are continuously pushed into the calling bucket. Take the above example, request is pushed into the calling bucket, and this function will be executed An http request (this http request will be implemented by the underlying module of Node.js). At the same time, the event of request completion is associated with a callback function. The request is popped out of the calling stack, and console.log is pushed into the calling stack to start execution. When the request is completed, the completion event is triggered and a message is added to the message queue. The message queue will first check whether the calling stacker is idle. If the calling stacker is not idle, it will wait until the calling stacker is idle and then The head of the message queue pops up, and the callback function associated with the message is executed.

Summary

The above is a conceptual summary of the non-blocking model and event loop. This event loop mechanism is not unique to Node.js, and the code of Node.js is executed in a single thread. What advantages does it have when facing a large number of concurrent requests?

The above picture shows the architecture diagram of Node.js. There is a module at the bottom of Node.js that is responsible for maintaining the thread pool. When an IO request is issued, the bottom module of Node.js will create a new thread to process the request. , and then return the results to the upper layer after completion. Then, when there are multiple requests, the underlying module of Node.js will use as few threads as possible to complete the most tasks. If there are idle threads, it will continue to be used to do other things, which is what I said earlier. In terms of opening a new process or thread for each request, it is undoubtedly much "smarter" and more efficient.

This article is a summary of learning Node.js. If there are any problems or deficiencies, please feel free to criticize and correct me.

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

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!

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 how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

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

See all articles