Event Queuing in NodeJS
Asked Answered
S

1

1

NodeJS uses a event driven model in which only one thread executes the events. I understand the first event executed will be the user JS code. Simple example from nodeJS website of a webserver is below

var http = require('http');

http.createServer(function (req, res) {

  res.writeHead(200, {'Content-Type': 'text/plain'});

  res.end('Hello World\n');

}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');

First event executed will perform the above steps. Later on the event loop will wait for events to be enqueued. My question is which thread enqueues event? Is there are a separate thread that does that? If yes can multiple threads enqueue events also?

Thank you.

Savonarola answered 17/5, 2015 at 19:58 Comment(8)
#18613523Strum
Not sure I get it? It's an event handler, whenever a request comes in on http://127.0.0.1:1337/ the events callback is executed. It's still single threaded, and event driven ?Hag
There are no threads (with one exception). See this for a low-level explanation of what's going on: #29884025Correctitude
I think my question was not clear. I know Event loop deques events and executes it. But I am trying to ask who enques event in the queue. If we consider the above example who will be listening for the request and enque the callback whenever that request is received.Savonarola
@ekhan: See my answer: it's the event loop thread. Is there anything about that explanation that's not clear?Correctitude
@ekhan: If we consider the example above, the callback is enqueued by the http.createServer function. Which runs in the main thread. Then at the end (after the console.log) the main thread has nothing else to execute so it waits for network I/O. When network I/O happens it dequeues and executes the callback. But in this case dequeue is perhaps a wrong word to use because the callback isn't really removed from the queue because it's an ongoing event handler, not a single use handler like setTimeout (ongoing, and single-use here are my words, don't really know the official names for these)Correctitude
Or maybe we have a misunderstanding of the words "enqueue" and "dequeue". What's your definition for these words?Correctitude
OK. I re-read the question and I think I understand the confusion. You're asking about the events themselves, not the callbacks. I'll update my answer.Correctitude
C
0

My question is which thread enqueues event? Is there are a separate thread that does that? If yes can multiple threads enqueue events also?

At it's core, the javascript interpreter is a loop around the select() sytem call. The select() system call informs the OS about the filehandles that your program is interested in and the OS will block execution of your program until there are events on any of those filehandles. Basically, select() will return if there's data on some I/O channel your program is interested in.

In pseudocode, it looks something like this:

while(1) {
    select(files_to_write,files_to_read,NULL,NULL,timeout)

    process_writable(files_to_write)
    process_readable(files_to_read)
    timeout = process_timers()
}

This single function allows the interpreter to implement both asynchronous I/O and setTimeout/setInterval. (Technically, this is only partly true. Node.js uses either select or poll or epoll etc. based on what functions are available and what functions are more efficient on your OS)

So, who enqueues events? The OS.


There is one exception. Disk I/O on node.js is handled by a separate thread. So for this I/O it is this disk I/O thread that enqueues events to the event loop.

It could be implemented without threads. For example, the Tcl programming language (which predates javascript but also has built-in event loop) implements disk I/O in the main thread using features such as kqueue (on BSD based OS like MacOS) or aio (on Linux and several other OSes) or overlapped-i/o (Windows). But the node.js developers simply chose threading to handle disk i/o.


For more how this works at the C level see this answer: I know that callback function runs asynchronously, but why?

Correctitude answered 17/5, 2015 at 20:15 Comment(4)
I think this answer has interesting information. Why someone down-vote it?Basrelief
@Basrelief I presume the OP downvoted my answer before I changed it. My previous answer focused more on how the callback gets called. He's more interested in how the event loop gets events. After I changed my answer I presume the OP is not satisfied because how it really works is in conflict with his world-view of how computers work so he didn't remove the downvote. That's a guess of course because votes are anonymousCorrectitude
I strongly recommend all programmers (yes, even PHP programmers) program in assembly (just not x86 assembly - it's horrible, maybe ARM or AVR) and try using interrupts at least once. Once you understand interrupts you understand how things like this works. Threads are not magic. They themselves are events to the kernel.Correctitude
Trying assembly is good. Anw, I think threads mostly are not events to the kernel (threads are in user space). right?Basrelief

© 2022 - 2024 — McMap. All rights reserved.