![]() If the poll queue was already empty then the event loop will look for requests in the next phase. Poll: The event loop will execute all the callbacks in poll queue synchronously. ![]() Idle, prepare: This phase is used internally by Node.This will be queued to execute in the pending callbacks phase. For example if a TCP socket receives ECONNREFUSED when attempting to connect, some *nix systems want to wait to report the error. Pending callbacks: This phase executes callbacks for some system operations such as types of TCP errors.I will execute it after 100ms as soon as possible.” The event loop will say, “OK, I am registering your request and I will not execute your code before 100ms for sure. Timers: Suppose you want to execute a code after 100ms with the help of setTimeout().When one of these operations completes, the kernel responds back to Node.js so that the appropriate callback may be added to the poll queue to eventually be executed. The event loop offloads operations to the system kernel whenever possible.This thread pool is composed of four threads which are used to delegate operations that are too heavy for the event loop. Else the task is assigned to background threads in the thread pool. If any C++ primitive is available then the event loop will assign the task to that primitive and the C++ code will complete the task in the main thread itself.Asynchronous request: If any asynchronous request is made then the event loop will check if there is any C++ primitive available to complete the task with the help of C++ libuv library.Synchronous request: The C++ code, under the hood, will execute any synchronous request then and there and return the response to the client.The event loop will check if the request is synchronous or asynchronous. Let’s dive deeper into the working of the event loop with a series of steps.Ī client makes a request to the server. The event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. It is like, if one car wants to halt then it is raised above in the air and asked to wait there while all the other cars can continue to move. It is asynchronous and can perform non-blocking I/O operations, like reading from the network, accessing a database or the filesystem. This means that any further execution is blocked until the file read operation is completed just like the whole traffic is blocked if a car needs to halt for a few seconds.īut, this is not what we observe while working with Node. Now suppose a client requested a file read task for a huge file which is going to take too long and due to single threading the event loop will have to wait for this process to finish before executing any other JavaScript code. So if you have tons of clients requesting your server then each of them will have to wait for their turn because the server is serving one client at a time using a single thread. Node.js is an open-source and cross-platform JavaScript runtime environment.Ī Node.js application is single threaded which means that there is just one thing happening at a time just like the one-lane road in the example above. How is this related to the node.js event loop? Let’s find out. But what if a car needs to halt on the road for a few seconds? Every car behind that car will have to wait and this will cause inconvenience for everyone. As long as all the cars are moving with an appreciable speed there will be no problem at all. Suppose you are driving on a one-lane road with tons of cars all lined up.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |