Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my activity monitor show Node.js using multiple threads?

I opened my activity monitory in OSX to to see how Node was getting along and to my surprise it's using 8 threads. How can in be!!?

Node with 8 threads

like image 572
jwerre Avatar asked Feb 07 '14 18:02

jwerre


People also ask

Does Nodejs use multiple threads?

Node. js runs JavaScript code in a single thread, which means that your code can only do one task at a time. However, Node. js itself is multithreaded and provides hidden threads through the libuv library, which handles I/O operations like reading files from a disk or network requests.

Is node JS really single threaded?

Node JS Platform does not follow Request/Response Multi-Threaded Stateless Model. It follows Single Threaded with Event Loop Model. Node JS Processing model mainly based on Javascript Event based model with Javascript callback mechanism.

What are threads in Nodejs?

Node. js uses two kinds of threads: a main thread handled by the event loop and several auxiliary threads in the worker pool. The event loop is the mechanism that takes callbacks (functions) and registers them to be executed at some point in the future. It operates in the same thread as the proper JavaScript code.

How does node js handle child threads?

Node. js is a single-threaded language and uses the multiple threads in the background for certain tasks as I/O calls but it does not expose child threads to the developer. But node. js gives us ways to work around if we really need to do some work parallelly to our main single thread process.


1 Answers

Node.js is single-threaded.

However, it is built on libuv which handles all low-level platform-dependent stuff, including async IO.

Now the issue is that there is no good API for async IO in operating systems nowadays. Different APIs exist, but they all have their issues.

So in order to implement cross-platform async API, libuv emulates it using a thread pool. This is where those threads are coming from.

like image 102
alex Avatar answered Sep 17 '22 08:09

alex