Ayush Sahu
Ayush Sahu

Reputation: 11

Event loop in Node.js

We all know that in Node.js, the functions are handled by worker thread for execution and then send to the event queue and then the event loop looks into the call stack. If the call stack is empty then the event loop takes the function's context environment to call stack, and then call stack process it and give it as a response.

My question is if we have multiple functions with same timeout function and then all the function is given to worker thread then worker thread sends their context environment to the event queue, and if the timeout of all the functions are same then they all come into the event queue at the same time and then if the call stack is empty then the event loop will send all the functions to call stack, and we all know the property of stack is FILO.

so if this happened resulting the last function should be sent in response first but this is not happening the first function is coming in response first if all the timeouts are the same?

Upvotes: 1

Views: 437

Answers (2)

Manjeet Singh
Manjeet Singh

Reputation: 2398

My question is if we have multiple functions with same timeout function

poll phase controll the timer

First read here

I highly recommend this series

So the main question is how does Node decide what code to run next?

As we know the Event Loop and the Worker Pool maintain queues for pending events and pending tasks, respectively.

don't confuse with Worker thread


Worker threads is different concept Read here

But In realtiy the Event Loop does not actually maintain a queue. Instead, it has a collection of file descriptors that it asks the operating system to monitor, using a mechanism like epoll (Linux), kqueue (OSX), event ports (Solaris), or IOCP (Windows). These file descriptors correspond to network sockets, any files it is watching, and so on. When the operating system says that one of these file descriptors is ready, the Event Loop translates it to the appropriate event and invokes the callback(s) associated with that event

The Worker Pool uses a real queue whose entries are tasks to be processed. A Worker pops a task from this queue and works on it, and when finished the Worker raises an "At least one task is finished" event for the Event Loop.

timer callback is called depends on the performance of the system (Node has to check the timer for expiration once before executing the callback, which takes some CPU time) as well as currently running processes in the event loop.

Upvotes: 0

jfriend00
jfriend00

Reputation: 708106

There are lots of things wrong in how you describe things in your question, but I'll speak to the timeout issue that you ask about.

nodejs has its own timer system. It keeps a sorted list of timers and the ONLY Javascript timeout that has a physical system timer is the next one to fire. If multiple Javascript timeouts are set to fire at the same point in time, then they all share that one OS timer.

When that OS timer fires and when the main JS thread is free and able to pull the next event from the event loop, it will see a JS timer is ready to call its callback. If there are more than one ready to go, all for the same time, then the interpreter will call each of their callbacks one after the other, in the order the timers were configured (FIFO).

We all know that in Node.js, the functions are handled by worker thread for execution and then send to the event queue and then the event loop looks into the call stack. If the call stack is empty then the event loop takes the function's context environment to call stack, and then call stack process it and give it as a response.

My question is if we have multiple functions with same timeout function and then all the function is given to worker thread

That part is wrong. They aren't given to a worker thread.

Then worker thread sends their context environment to the event queue, and if the timeout of all the functions are same then they all come into the event queue at the same time

As I described above, timers are a special type of event in the event loop code. They use only one system timer at a time to schedule the next timer to fire. Multiple timers set to fire at the same time are all stored in the same list (a list element for that particular time) and all share the same OS timer when it's their turn to be next. So, they don't all come into the event queue at the same time. Nodejs has set one system timer for the group of timers set to fire at the same time. When that system timer fires and when the interpreter is free to pull the next event from the event loop, then it will call each callback for each timer set for that time one after another in FIFO order.

and then if the call stack is empty then the event loop will send all the functions to call stack, and we all know the property of stack is FILO.

I don't know what "send all the functions to that call stack" means. That's not how things work at all. node.js runs your Javascript in a single thread (except for WorkerThreads which are not in play here) so it calls the callback for one timer, runs that to completion, then calls the callback for the next timer, runs it to completion and so on...

so if this happened resulting the last function should be sent in response first but this is not happening the first function is coming in response first if all the timeouts are the same?

As I've said a couple times above, multiple timers set to fire at the same use one system timer and when that system timer fires, the callbacks for each of those timers are called one after the other in FIFO order.


References

You can learn more about the node.js timer architecture here: How does Node.js manage timers.

And, here's some more info about the node.js timer architecture taken from comments in the source code: How many concurrent setTimeouts before performance issues?.

Looking for a solution between setting lots of timers or using a scheduled task queue

Six Part Series on the Node.js Event Loop and How It Works

Upvotes: 2

Related Questions