


What is an event loop? Detailed explanation of the event loop in Node.js
What is an event loop? This article will introduce you to the event loop in Node, I hope it will be helpful to you!
What is an event loop?
Although JavaScript is single-threaded, the event loop uses the system kernel as much as possible to allow Node.js to perform non-blocking I/O operations Although most modern kernels are multi-threaded, they can handle multi-threaded tasks in the background. When a task is completed, the kernel tells Node.js, and then the appropriate callback will be added to the loop for execution. This article will introduce this topic in further detail
Time Loop Explanation
When Node.js starts executing, it will first initialize the event loop and process the provided input script (or put it into the REPL, which is not covered in this document). This will execute asynchronous API calls and schedule timings. handler, or call process.nextTick(), and then start processing the event loop
The following figure shows a simplified overview of the event loop execution sequence
┌───────────────────────────┐ ┌─>│ timers │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐ │ │ pending callbacks │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐ │ │ idle, prepare │ │ └─────────────┬─────────────┘ ┌───────────────┐ │ ┌─────────────┴─────────────┐ │ incoming: │ │ │ poll │<─────┤ connections, │ │ └─────────────┬─────────────┘ │ data, etc. │ │ ┌─────────────┴─────────────┐ └───────────────┘ │ │ check │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐ └──┤ close callbacks │ └───────────────────────────┘
Each box represents an event loop A stage
Each stage has a FIFO queue callback execution. However, each stage is executed in its own way. Generally speaking, when the event loop enters a stage, it will execute the current For any operation in a stage, callbacks in the queue of the current stage will be executed until the queue is completely consumed or the maximum data of the queue is reached. When the queue is exhausted or reaches its maximum size, the event loop moves to the next phase.
Phase Overview
- timers This stage executes the callbacks of setTimeout() and setInterval()
- pending callbacks Delay execution of I/O callbacks until the next loop iteration
- idle,prepare Internal use only
- poll Retrieve new I/O events; execute I/O related callbacks (almost all related callbacks, close callbacks,)
- check setImmediate() will be here Phase calls
- close callbacks Close callbacks, for example: socket.on('close', ...)
In each process of the event loop , Node.js checks if it is waiting for asynchronous I/O and timers, if not then shuts down completely
Phase Details
timer
A timer specifies the critical point at which a callback will be executed, rather than the time one wants it to execute. The timer will execute as early as possible after the specified elapsed time. However, operating system scheduling or other callbacks can delay its execution.
Technically speaking, the poll phase determines when the callback is executed
For example, you set a timer to execute after 100 ms, but your script reads a file asynchronously It took 95ms
const fs = require('fs'); function someAsyncOperation(callback) { // Assume this takes 95ms to complete fs.readFile('/path/to/file', callback); } const timeoutScheduled = Date.now(); setTimeout(() => { const delay = Date.now() - timeoutScheduled; console.log(`${delay}ms have passed since I was scheduled`); }, 100); // do someAsyncOperation which takes 95 ms to complete someAsyncOperation(() => { const startCallback = Date.now(); // do something that will take 10ms... while (Date.now() - startCallback < 10) { // do nothing } });
When the event loop enters the poll phase, there is an empty queue (fs.readFile() has not completed yet), so it will wait for the remaining milliseconds until the fastest timer threshold Arrives, when 95 ms later, fs.readFile() completes reading the file and will take 10 ms to complete adding to the poll phase and completing execution. When the callback is completed, there are no callbacks in the queue to be executed, and the event loop returns to the timers phase. Execute the timer's callback. In this example, you will see that the timer is delayed for 105 ms and then executed
To prevent the poll phase from blocking the event loop, libuv (the C language library that implements the event loop and all asynchronous behavior on the platform) There is also a maximum value in the poll phase to stop polling for more events
pending callbacks
This phase executes callbacks for certain system operations (such as TCP error types). For example, some *nix systems want to wait for an error to be reported if a TCP socket receives ECONNREFUSED when trying to connect. This will be queued for execution during the pending callback phase.
poll
The poll phase has two main functions
- Calculate the I/O blocking time
- Execution Events in the poll queue
When the event loop enters the poll phase and there is no timer, the following two things happen
- If the poll queue is not empty, the event loop Each callback will be executed synchronously and iteratively until all are executed, or the system's hard limit is reached
- If the poll queue is empty, the following two situations will occur
- If it is a setImmediate callback, The event loop will end the poll phase and enter the check phase to execute the callback
- If it is not setImmediate, the event loop will wait for the callback to be added to the queue, and then execute it immediately
Once the poll queue is empty, the event loop will detect whether the timer has expired. If so, the event loop will reach the timers stage to execute the timer callback
check
此阶段允许人们在 poll 阶段完成后立即执行回调。 如果轮询阶段变得空闲并且脚本已使用 setImmediate() 排队,则事件循环可能会继续到 check 阶段而不是等待。
setImmediate() 实际上是一个特殊的计时器,它在事件循环的单独阶段运行。 它使用一个 libuv API 来安排在 poll 阶段完成后执行的回调。
通常,随着代码的执行,事件循环最终会到达 poll 阶段,它将等待传入的连接、请求等。但是,如果使用 setImmediate() 安排了回调并且 poll 阶段变得空闲,它将结束并继续 check 阶段,而不是等待 poll 事件。
close callbacks
如果一个 socket 或者操作突然被关闭(e.g socket.destroy()),close 事件会被发送到这个阶段,否则会通过process.nextTick()发送
setImmediate() VS setTimeout()
setImmediate() 和 setTimeout() 是相似的,但是不同的行为取决于在什么时候被调用
- setTimmediate() 在 poll 阶段一旦执行完就会执行
- setTimeout() 在一小段时间过去之后被执行
每个回调执行的顺序依赖他们被调用的上下本环境,如果在同一个模块被同时调用,那么时间会受到进程性能的限制(这也会被运行在这台机器的其他应用所影响)
例如,如果我们不在I/O里边运行下面的脚本,尽管它受进程性能的影响,但是不能够确定这两个计时器的执行顺序:
// timeout_vs_immediate.js setTimeout(() => { console.log('timeout'); }, 0); setImmediate(() => { console.log('immediate'); });
$ node timeout_vs_immediate.js timeout immediate $ node timeout_vs_immediate.js immediate timeout
然而,如果你移动到I/O 循环中,immediate 回调总是会先执行
// timeout_vs_immediate.js const fs = require('fs'); fs.readFile(__filename, () => { setTimeout(() => { console.log('timeout'); }, 0); setImmediate(() => { console.log('immediate'); }); });
$ node timeout_vs_immediate.js immediate timeout $ node timeout_vs_immediate.js immediate timeout
setImmediate 相对于 setTimeout 的优势是 setImmediate 如果在I/O 中总是会优先于任何计时器被先执行,与存在多少计时器无关。
process.nextTick()
尽管 process.nextTick() 是异步API的一部分,但是你可能已经注意到了它没有出现在图表中,这是因为 process.nextTick() 不是事件循环技术的一部分,相反,当前操作执行完毕之后 nextTickQueue 会被执行,无论事件循环的当前阶段如何。 在这里,操作被定义为来自底层 C/C++ 处理程序的转换,并处理需要执行的 JavaScript。 根据图表,你可以在任意阶段调用 process.nextTick(),在事件循环继续执行之前,所有传递给 process.nextTick() 的回调都将被执行,这个会导致一些坏的情况因为它允许你递归调用 process.nextTick() "starve" 你的 I/O ,这会阻止事件循环进入 poll 阶段。
为什么这会被允许
为什么这种情况会被包含在Node.js中?因为Node.js的设计理念是一个API应该总是异步的即使它不必须,看看下面的片段
function apiCall(arg, callback) { if (typeof arg !== 'string') return process.nextTick( callback, new TypeError('argument should be string') ); }
该片段会进行参数检查,如果不正确,它会将错误传递给回调。 API 最近更新,允许将参数传递给 process.nextTick() 允许它接受在回调之后传递的任何参数作为回调的参数传播,因此您不必嵌套函数。
我们正在做的是将错误传回给用户,但前提是我们允许用户的其余代码执行。 通过使用 process.nextTick(),我们保证 apiCall() 总是在用户代码的其余部分之后和允许事件循环继续之前运行它的回调。 为了实现这一点,允许 JS 调用堆栈展开,然后立即执行提供的回调,这允许人们对 process.nextTick() 进行递归调用,而不会达到 RangeError:从 v8 开始超出最大调用堆栈大小。
更多node相关知识,请访问:nodejs 教程!
The above is the detailed content of What is an event loop? Detailed explanation of the event loop in Node.js. For more information, please follow other related articles on the PHP Chinese website!

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.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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