In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of the various stages of Event Loop in Node.js, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Event Loop phase description diagram
Timers
The timer phase handles setTimeout on the setInterval callback, and the timing of starting processing is associated with the poll phase.
Pending callbacks
This phase performs callbacks for certain system operations, such as when the TCP socket receives an ECONNREFUSED when connecting.
There are some articles on the Internet that refer to this stage as I callbacks O callbacks, which are outdated and erroneous. You can go to the issue under the official Node.js library: # 1118.
Idle, prepare
Internal use, ignore.
Poll
Poll is a core phase, waiting for the new Imax O event to be triggered, as well as performing a callback associated with it. The vast majority of cases where asynchrony occurs in Node.js are I _ peg O operations, and their callbacks are basically executed at this stage.
The poll phase mainly does two things:
Calculate how long you need to wait for the new Ithumb O event
When entering the poll phase, if the team is listed as empty and there is no setImmediate and ready timer,Node.js will wait here for a certain amount of time for the new Ipoll O event to arrive, and then immediately execute its callback. It is not specific how long the block waits for this situation, but if there is no newly arrived block O event after a certain period of time, it is certain that the loop will still enter the check phase or return to the timer phase.
Handle events in the queue at this stage
When entering the poll phase, if the queue is not empty and the timer,Node.js is not ready, the callback in the queue will be executed here until the queue is empty or the number of callback executed reaches a certain value set by the system. Then Node.js checks to see if there is a preset setImmediate, and if so, enter the check phase, otherwise start checking timer readiness and choose to return to the timer phase or enter the check phase.
For the poll phase, some details are not clear by reading the official documentation and are expressed in pseudocode:
Enter pool phase:if (has timer scheduled) {/ / there is no official mention of what will be done in this situation} else {if (isEmpty (queue)) {if (has (setImmediate)) {/ / enter the check phase} else if (! isEmpty (timer)) {/ / return to the timer phase} else {/ / wait for a new Imax O event / / The new Icano event triggers the callback to be executed immediately. The logic after the completion of the execution is not clear} / / at present, it seems that only setImmediate will enter the check phase, which must be unreasonable} if (! isEmpty (queue)) {let result = execute (queue) If (result = = 'queue is empty') {/ / there is no follow-up logic / / guess is to return to the processing logic where the queue is empty} if (result =' reached hard limit') {/ / the subsequent logic here / / may be treated like queue is empty}
The point of confusion is that the timing and direction of coming out of the poll phase is not very clear, but at my current level and energy, it can only go so far.
Check
When the poll phase execution is completed, the check phase execution will be entered, and the execution content of this phase is all setImmediate callbacks.
Close callbacks
The socket is abnormally closed, and the callback for the 'close' event is executed at this stage.
Process.nextTick
Process.nextTick is often used to make asynchronous calls, but it is not part of the event loop. The callback in process.nextTick is placed in nextTickQueue waiting for the "current operation" to be processed immediately after the completion of the "current operation". It has nothing to do with the stage in the event loop. The original definition of the current operation is: "An operation is defined as a transition from the underlying C handler + handler, and handling the JavaScript that needs to be executed." This means that nextTickQueue is processed when a piece of Javascript code completes the execution of the switch to the Candlestick Cobb + layer.
The article mentioned that a special case is Deduplication, which is an internal optimization feature of Node.js. When there are multiple callbacks that need to be performed at the same time in the timer and check phases, the switch will only occur once, so the execution of nextTick callbacks seems to be delayed in this case.
Code example:
SetImmediate (() = > {console.log ('1'); process.nextTick (() = > console.log ('2'));}); setImmediate () = > {console.log ('3'); process.nextTick (() = > console.log ('4'));})
There are two setImmediate. After entering the check phase, the switch will not occur until all the callback codes of the setImmediate are executed, so the nextTick callback will be executed. Therefore, the running result of the above code is: "1-3-2-4". Except for the above scenarios, nextTick will be executed before setImmediate.
The above is all the content of the article "sample Analysis of each stage of Event Loop in Node.js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.