Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NodeJS really Single-Threaded?

Node.js solves "One Thread per Connection Problem" by putting the event-based model at its core, using an event loop instead of threads. All the expensive I/O operations are always executed asynchronously with a callback that gets executed when the initiated operation completes.

The Observation IF any Operation occurs is handled by multiplexing mechanisms like epoll().

My question is now:

  • Why doesn't NodeJS block while using the blocking Systemcalls select/epoll/kqueue?

  • Or isn't NodeJS single threaded at all, so that a second Thread is
    necessary to observe all the I/O-Operations with select/epoll/kqueue?

like image 693
Filipe Santos Avatar asked Aug 10 '11 21:08

Filipe Santos


People also ask

Does node js use multiple threads?

Node. js is a free, cross-platform JavaScript runtime environment, while single-threaded in nature, executes asynchronous code using several thread instances in the background. Because of its design, Node. js has received a lot of flak.

Is node js single threaded medium?

Yes, you can. On a Windows machine, you can do it by setting it before calling the script. In this case, you can see that it takes approximately one second(around 0.9s) for the first execution of “pbkdf2”.

Is node JS asynchronous single threaded?

Node. js is Single Threaded, i.e. it executes the code in a single sequence or direction. At a given time, only a single task/ call is executed. Asynchronous and Single-Threaded: Execution doesn't wait for the current request to complete and moves to the next request/call.

How many threads does node js use?

Conclusion. Node. js has two types of threads: one Event Loop and k Workers.


2 Answers

NodeJS is evented (2nd line from the website), not single-threaded. It internally handles threading needed to do select/epoll/kqueue handling without the user explicitly having to manage that, but that doesn't mean there is no thread usage within it.

like image 171
Femi Avatar answered Sep 20 '22 00:09

Femi


No.

When I/O operations are initiated they are delegated to libuv, which manages the request using its own (multi-threaded, asynchronous) environment. libuv announces the completion of I/O operations, allowing any callbacks waiting on this event to be re-introduced to the main V8 thread for execution.

V8 -> Delegate I/O (libuv) -> Thread pool -> Multi threaded async

like image 39
serkan Avatar answered Sep 17 '22 00:09

serkan