Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many threads per Node.js app?

Tags:

node.js

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
like image 438
sof Avatar asked Dec 08 '25 18:12

sof


1 Answers

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.

like image 158
Yuri Zarubin Avatar answered Dec 11 '25 13:12

Yuri Zarubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!