sof
sof

Reputation: 9649

How many threads per Node.js app?

It's often said that Node.js app is single-threaded, however there're total 3 threads resulted from the commands below. What are they?

$ node -e 'while (true) {}' &
=> <node_pid>
$ ps huH p <node_pid> | wc -l
=> 3

Upvotes: 3

Views: 3067

Answers (2)

Shahin Ghasemi
Shahin Ghasemi

Reputation: 1759

Your javascript runs on a single-thread environment while under the hood you have libuv which can manage threads. even though libuv can create thread pool but nowadays most of the OS have threads interfaces available.

In other words libuv will use these interfaces prior to thread pools and if they are not available, libuv will manage these threads using the thread pool.

Upvotes: 1

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

The application JavaScript code you write is single-threaded as Node uses callbacks to deal with blocking IO, which are then processed in order by a single event loop. However, all of this is executed by the underlying platform written in C++, the V8 engine, as well as the libuv library written in C. These two components do not share the constraints of the event loop, and are able to spawn multiple threads.

Upvotes: 9

Related Questions