Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poll phase in NodeJS event loop

I was going through node docs for event loop and I got very confused. It says -

timers: this phase executes callbacks scheduled by setTimeout() and 
setInterval().
I/O callbacks: executes almost all callbacks with the exception of close callbacks, the ones scheduled by timers, and setImmediate().
idle, prepare: only used internally.
poll: retrieve new I/O events; node will block here when appropriate.
check: setImmediate() callbacks are invoked here.
close callbacks: e.g. socket.on('close', ...).

Then in detailed poll phase, they say that it executes timers scheduled with timer and also process i/o events in poll queue. My confusion is taht we already have timer phase and i/o callback phase for those callbacks, then what is the work done by poll phase. It also says that thread may sleep in poll phase but I don't get it properly.
My questions are-

  1. Why poll phase is executing scripts for timers and i/o(s) when we already have timer and i/o callback phase ?
  2. Is it like poll phase executes callbacks on behalf of timer and i/o callback phase and timer and callback phase is only for internal processing no callbacks are executed in this phase ?
  3. Where can we place promises in this loop ? Earlier I thought that promises can be thought simply as callbacks and we can treat them like callbacks only, but in this video, he says that promises goes into an internal event loop, but does not talk in detail.

I am very confused at this point. Any help will be appreciated.

like image 261
Krrish Raj Avatar asked Sep 29 '17 09:09

Krrish Raj


People also ask

What is POLL phase in event loop?

Understanding the event loop. The Poll phase is responsible for blocking the current flow of the code to check for incoming asynchronous events or timers. It checks on the response of any of the handlers it's been given (file handlers, socket handlers) and appends them to the Callbacks phase queue.

How the event loop works in NodeJS?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.

What is the difference between process nextTick () and setImmediate ()?

process. nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. setImmediate() method is used to execute a function right after the current event loop finishes.


2 Answers

The poll phase boils down to an asynchronous I/O wait. Libuv will use different APIs depending on the OS but they all generally have the same pattern. I'm going to use select() as an example.

The poll is basically a system call like this:

select(maxNumberOfIO, readableIOList, writableIOList, errorIOList, timeout);

This function blocks. If no timeout value is specified it blocks forever.

The result is that node.js will not be able to execute any javascript as long as there is no I/O activity. This obviously makes it impossible to execute time-based callbacks like setTimeout() or setInterval().

Therefore, what node needs to do before calling such a function is to calculate what value to pass as timeout. It generally does this by going through the list of all timers and figure out the shortest amount of time it can wait for I/O (the next nearest timer) and use that as the timeout value. It basically processes all the timers but not to execute their callbacks, it does it to figure out the wait time.

like image 192
slebetman Avatar answered Sep 22 '22 11:09

slebetman


Nodejs has 5 major phases.
1) timers phase.
2) pending call back phase.
3) poll phase
4) check (set immediate).
5) close

Answer to your questions.
1)The call backs to timers and check phase are executed in their respective phases and not in poll phase.

2)All the I/o related call backs and other are executed in the poll phase. The pending call back phase is only for system level callbacks like tcp errors, none of our concern

3)After each phase, node js has an internal event loop which resolves all the process.nextTick callbacks, and another smaller event loop which executes the resolved promises then callbacks i.e Promise.resolve.then() callbacks.

like image 20
Sagar Chaudhary Avatar answered Sep 21 '22 11:09

Sagar Chaudhary