Nodejs Thread pool
Yes in Nodejs your code runs only on a single thread, however this does not restrict the Node from creating new threads for certain operations, if necessary.
The concept you have to understand is that Nodejs processes a list of events, this is known as Event loop. A simple way of understanding the Event loop is for example on a server HTTP
. When a request HTTP
get to your Node server a new item is inserted to process in the event list. And at a certain point the Node will call the code you have registered to handle that event.
Time-consuming operations can and should make use of the Node event loop. The goal is that if an operation takes too long, the Event loop Node can continue running while this operation is processed.
When the operation completes it will record an event on Event loop and once again your code will run on the same thread as always.
UV_THREADPOOL_SIZE
General rule on setting certain default values specified by the platform is that you should not change them.
Before changing any of these values in order to optimize performance you should be rigorous in your tests and use a profiler to check if you get any benefits.
SOEN - When is the thread pool used
There is an untruth in this question, there is the possibility of creating multiple "threads" in js. see: 1,2
– MarceloBoni
Works identical to any other resource pool scheme. Similar to
RunnerPool
(or any similar Java name)– Jefferson Quesado
@Marceloboni print my or the two links are equal?
– Costamilam
@Jeffersonquesado thanks, can tell me about the use of
UV_THREADPOOL_SIZE
?– Costamilam
@Guilhermecostamilam http://docs.libuv.org/en/latest/threadpool.html studying to give a more assertive answer
– Jefferson Quesado
I was wrong to paste the second link 2*
– MarceloBoni
@Marceloboni seems to me wrong to say that it is possible to clear multiple threads in javascript, despite being able to run a part of the application alone, it would not be
.js
, on Node, for example, and say that javascript is multi thread– Costamilam
So the emphasis on threads in my comment, is not exactly the same method, but emulates well the behavior, depending on what you want to do, I believe the main difference is that in other types of languages you break the task into mini-tasks, in js you do not break the task, but you create other tasks that perform simultaneously. Read more here.
– MarceloBoni