In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the stages of node.js event queue". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what are the stages of node.js event queue"!
The node.js event queue has six phases: 1,"timers" phase;2,"I/O callbacks" phase;3,"idle, prepare" phase;4,"poll" phase;5,"check" phase; and 6,"close callbacks" phase.
This tutorial operating environment: Windows 7 system, nodejs version 12.19.0, DELL G3 computer.
The differences between the event queue in Node and the browser are still relatively large, but the common point is that the mechanism of macro tasks and micro tasks is the same, as shown in the figure about the macro task and micro task classification of Node.
Nexttick is executed first every time a node event opens (microtask has highest priority).
The node event queue can be divided into the following six stages:
1. timers stage: this stage executes the callback of timer (setTimeout, setInterval)
2. I/O callbacks stage: perform some system call errors, such as error callbacks for network communication
idle, prepare phase: only used internally within node
Poll phase: Get new I/O events, node will block here under appropriate conditions
5. check phase: execute callback of setImmediate()
6. close callbacks stage: close event callback of socket is executed
Let's focus on the three phases of timers, poll, and check, because most asynchronous tasks in daily development are handled in these three phases.
timers phase
timers is the first stage of the event loop, Node will check whether there is an expired timer, if there is, it will push its callback into the timer's task queue waiting for execution, in fact, Node does not guarantee that the timer will be executed immediately after the preset time, because Node's expiration check on the timer is not necessarily reliable, it will be affected by other running programs on the machine, or the main thread is not idle at that point in time. For example, in the following code, setTimeout() and setImmediate() are executed in an indeterminate order.
setTimeout(() => { console.log('timeout')}, 0)setImmediate(() => { console.log('immediate')})
For example, if the previous code is a pit, the node cannot determine the exact time for each event queue to be established, which may be 5ms this time (timer is pushed into the stack and executed in the second round) and 1ms the next time (timer is captured and executed in the first round). Therefore, the output sequence is uncertain, which is also the specific reason, and when nested an asynchronous operation wraps them, it is 100% guaranteed that the immediate will execute first.
fs.readFile('./ index.html',(err,result)=>{ setTimeout(() => { console.log('timeout') }, 0) setImmediate(() => { console.log('immediate') })})
This is because the timer is not captured in the first round, so the io callback capture is performed directly at the poll stage. When the io ends and enters the check stage, the immediate will be executed, and the timeout will be executed at the very beginning of the second round.
poll phase
The poll phase has two main functions:
1. Handling events for poll queues
2. When a timer has timed out, execute its callback function
Even loop will synchronously execute callbacks in the poll queue until the queue is empty or the callbacks executed reach the system limit (the exact upper limit is unknown), and then even loop will check whether there is a default setImmediate(), divided into two cases:
1. If there is a default setImmediate(), event loop will end the poll phase and enter the check phase, and execute the task queue of the check phase.
2. If there is no default setImmediate(), the event loop will block waiting at that stage
Note a detail, no setImmediate() will cause the event loop to block in the poll phase, so the timer set before will not be executed? Therefore, in the poll phase event loop will have a check mechanism to check whether the timer queue is empty, if the timer queue is not empty, the event loop will start the next round of event loop, that is, re-enter the timer phase.
the check phase
The callback of setImmediate() will be added to the check queue, and you can see from the phase diagram of the event loop that the check phase is executed after the poll phase.
At this point, I believe that everyone has a deeper understanding of "what are the stages of the node.js event queue", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.