As far as I know, all IO requests and other asynchronous tasks are done by libuv
in nodejs
.
I want to know if libuv
is using threading. If it is, is it using all available core or not?
First of all, what is libuv
. As mentioned in the documentation, it's a multi-platform support library with a focus on asynchronous I/O.
libuv
doesn't use thread for asynchronous tasks, but for those that aren't asynchronous by nature.
As an example, it doesn't use threads to deal with sockets, it uses threads to make synchronous fs calls asynchronous.
When threads are involved, libuv
uses a thread pool the size of which you can change at compile-time using UV_THREADPOOL_SIZE
.node.js
is provided with a precompiled version of libuv
and thus a fixed UV_THREADPOOL_SIZE
parameter.
It goes without saying that it has nothing to do with the number of cores of your chip.
I'm tempted to affirm that you can safely ignore the topic, for libuv
and thus node.js
don't use threads intensively for their purposes (unless you are using them in a really perverse way or if you are running an high number of libuv
work requests).
Feel free to run an instance of node.js
per core if you need as most of the users do.
The design overview section of libuv
is also clear enough about this point:
The I/O (or event) loop is the central part of libuv. It establishes the content for all I/O operations, and it’s meant to be tied to a single thread. One can run multiple event loops as long as each runs in a different thread.
The libuv module has a responsibility that is relevant for some particular functions in the standard library. for SOME standard library function calls, the node C++ side and libuv decide to do expensive calculations outside of the event loop entirely.They make something called a thread pool that thread pool is a series of four threads that can be used for running computationally intensive tasks such as hashing functions.
By default libuv creates four threads in this thread pool. So that means that in addition to that thread used for the event loop there are four other threads that can be used to offload expensive calculations that need to occur inside of our application. Many of the functions include in the node standard library will automatically make use of this thread pool.
If you have too many function calls, It will use all of the cores. CPU cores do not actually speed up the processing function calls, they just allow for some amount of concurrency inside of the work that you are doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With