简体   繁体   中英

What is node.js event loop itself?

I have read a lot of guides and resources about event loop but I still do not know what is the event loop itself ?

I know it is implemented by libuv library but what is that thing which is implemented using that library ? Is it a function, an object or it is just the library itself ?

The guides say that the event loop runs in the same thread with my application code, so does this mean that event loop is a javascript code, too ?

If the event loop runs with my code in the same thread, how it can keep looping on the phases while my code is in execution or it just loops only after my code finishes its execution.

I understand the general idea of event loop but when it comes to the details it actually confuses me and I have read a lot but I am still confused.

Whenever we start up a node program, node automatically creates one thread and then executes all of our code. Inside of the single thread there is something called event loop . You can think of the event loop as being like a control structure that decides what our one thread should be doing at any given point in time.This event loop is the absolute core of every program that we run and every program that we run has exactly one event loop.

After first line of code is executed we enter in event-loop. This event loop is like a WHILE loop. so this event loop or this while loop essentially is going to execute again and again. So every single time the event loop runs inside of our node application we refer to that as one tick . Entire body executes in one tick.

Event Loop Tick: What occurs during every single tick?

1-So the very first thing occurs is node looks at all the different functions that have been passed to set time out and set interval . If any of those timers have expired the node calls the relevant callbacks associated with each one.

2-for the second step node looks at pending OS tasks and pending operations and calls relevant callbacks. So in the second step node will look at that collection of all the different tasks and operations and if any of these things have been completed or any new events have been triggered the node executes the relevant callbacks.

3-In this step node actually pauses execution temporarily. So node just sits here it pauses and it says that "I will continue whenever some number of events occur". So during this pause phase node decides that its not going to just run through the event loop as fast as it possibly can. A while loop would execute as fast as it possibly could but thats not actually what occurs inside the event loop. This pause during step 3 just sits around and says you know what I've got no other work to do. I'm just going to wait until I see that a new pending task has been completed.

4-Node again looks at Pending timers in step 1. But in this case node does not care about set time out and set interval function calls. It only looks for functions that have been registered with setImmediate()

5-It handles any close events. Essentially this last step inside the event loop is about just handling cleanup code and cleaning up after ourselves.

Every while() loop has to have some type of condition tied to it. We put that condition inside parentheses while(condition) and whenever that condition returns false the while loop is no longer going to execute. That same idea applies to the event loop as well. So every single time that the event loop is about to execute, node first does a quick check to decide whether or not it should allow the loop to proceed for another iteration. If node decides that the loop should not be entered again than the entire event loop gets skipped. Node does three separate checks to decide whether or not the event loop should continue for another iteration.

First check is to look to see if there are still any functions that have been registered with seTimeout, setInterval or setImmediate and still need to be executed.

Second is to see if there are any pending operating system tasks an example of an operating system task is something like an HTTP server listening to requests on some port.

Third if there are any long running operations that are still being executed inside our program. An example of a long running operation would be something like a function call inside the FS module to read some file off the hard drive.


The libuv module has a responsibility that is relevant for some particular functions in the standard library. For SOME standard library function calls, the node C++ side and libuv decide to do expensive calculations outside of the event loop entirely. They make something called a thread pool that thread pool is a series of four threads that can be used for running computationally intensive tasks such as hashing functions.

By default libuv creates four threads in this thread pool. So that means that in addition to that thread used for the event loop there are four other threads that can be used to offload expensive calculations that need to occur inside of our application. Many of the functions include in the node standard library will automatically make use of this thread pool.

Now the presence of this thread pool is very significant. Well clearly Node.js is not truly single threaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM