2

When Node.js starts, it initializes the event loop, processes the provided input script which may make async API calls, schedule timers, or call process.nextTick(), then begins processing the event loop.

There are seven phases and each phase has its own event queue which is based on FIFO.

So application makes a request event, event demultiplexer gathers those requests and pushes to respective event queues.

For example, If my code makes two reqeusts one is setTimeOut() and another is some API Call, demultiplexer will push the first one in timer queue and other in poll queue.

But events are there, and loop watches over those queues and events, on completion in pushes the registered callback to the callstack where it is processed.

My question is,

1). Who handles events in event queue to OS?

2). Does event loop polls for event completion in each event queue or does OS notifies back?

3). Where and who decides whether to call native asyncrhonous API or handle over to a thread pool?

I am very verge of understanding this, I have been strugling a lot to grasp the concepts. There are a lot of false information about node.js event loop and how it handles asynchronous calls using one thread.

Please answer this questions if possible. Below are the references where I could get some better insight from.

https://github.com/nodejs/nodejs.org/blob/master/locale/en/docs/guides/event-loop-timers-and-nexttick.md

https://dev.to/lunaticmonk/understanding-the-node-js-event-loop-phases-and-how-it-executes-the-javascript-code-1j9

how does reactor pattern work in Node.js?

https://www.youtube.com/watch?v=PNa9OMajw9w&t=3s

confusedWarrior
  • 938
  • 2
  • 14
  • 30
  • What specific type of OS action are you talking about? Different types of operations work differently. – jfriend00 Jan 06 '21 at 03:25
  • 1: V8, 2: it depends on event 3: libuv (primarily) – Randy Casburn Jan 06 '21 at 03:40
  • I marked your question as duplicate. I hope my answer in the duplicate question is satisfactory. There are indeed a lot of false information about node.js event loop but not about it being handled in a single thread but that it need multiple threads/thread pool to handle events. Events are handled in a single thread. – slebetman Jan 06 '21 at 03:55
  • .. Node.js additionally spawns additional threads for only 4 things: disk I/O (because it's hard to write compatible single-threaded code across various OSes, hard but not impossible - see the Tcl language for a pure single thread implementation), encryption (because it is CPU not I/O), DNS (because there are no cross-platform async DNS library) and zip compression (again it is CPU not I/O) – slebetman Jan 06 '21 at 03:55
  • @slebetman - I reopened because I do not see how this question is a dup of that other question. Just because you might cover some of this answer in your answer to that other question does not make this question a duplicate of that other question. In fact, they were very different questions. – jfriend00 Jan 06 '21 at 03:56
  • @jfriend00 Which part of this question is not answered by explaining interrupts and OS event queue? – slebetman Jan 06 '21 at 03:58
  • See this node.js documentation (from node.js) for more information on all the threads that node.js run and what runs on the main thread: https://nodejs.org/en/docs/guides/dont-block-the-event-loop/#what-code-runs-on-the-worker-pool – slebetman Jan 06 '21 at 03:59

1 Answers1

1

Who handles events in event queue to OS?

How OS events work depends upon the specific type of event. Disk I/O works one way and Networking works a different way. So, you can't ask about OS events generically - you need to ask about a specific type of event.

Does event loop polls for event completion in each event queue or does OS notifies back?

It depends. Timers for example are built into the event loop and the head of the timer list is checked to see if it's time has come in each timer through the event loop. File I/O is handled by a thread pool and when a disk operation completes, the thread inserts a completion event into the appropriate queue directly so the event loop will just find it there the next time through the event loop.

Where and who decides whether to call native asynchronous API or handle over to a thread pool?

This was up to the designers of nodejs and libuv and varies for each type of operation. The design is baked into nodejs and you can't yourself change it. Nodejs generally uses libuv for cross platform OS access so, in most cases, it's up to the libuv design for how it handles different types of OS calls. In general, if all the OSes that nodejs runs on offer a non-blocking, asynchronous mechanism, then libuv and nodejs will use it (like for networking). If they don't (or it's problematic to make them all work similarly), then libuv will build their own abstraction (as with file I/O and a thread pool).


You do not need to know the details of how this works to program asynchronously in nodejs. You make a call and get a callback (or resolved promise) when its done, regardless of how it works internally. For example, nodejs offers some asynchronous crypto APIs. They happen to be implemented using a thread pool, but you don't need to know that in order to use them.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks for taking time and giving some wonderful explanation. For _Who handles events in event queue to OS?_ My intent was to ask, who process those events from event queue? Does libuv do that? If an event is there it must be acknowledged and be processed. I know event loop is also part of libuv but there is a lot more. So who acknowledges those events, libuv(some other part) or event loop? I am talking about events processing not the callback after completion. – confusedWarrior Jan 06 '21 at 03:57
  • @confusedWarrior - What do you mean "acknowledges those events"? In nodejs, an event always calls a callback function in Javsacript and that's how the calling code gets notified of completion or error when the event loop processes the completion of the event. Some nodejs APIs now wrap that callback in a promise and the API in nodejs is promise-based, but at the event loop level in libuv, everything is still a callback. – jfriend00 Jan 06 '21 at 03:59
  • @confusedWarrior If that was your intent see my answer to this question (I previously marked it as duplicate to this question): https://stackoverflow.com/questions/61262054/is-there-any-other-way-to-implement-a-listening-function-without-an-infinite-w/61826079#61826079 – slebetman Jan 06 '21 at 04:01
  • @jfriend00 Apparently my marking as duplicate was a correct interpretation of the OP's core question – slebetman Jan 06 '21 at 04:01
  • Wow, thanks a lot that answer is eye opening. My eyes are wide open now ಠ_ಠ, I can see clearly from hardwares to OS to my application – confusedWarrior Jan 06 '21 at 04:18