How the Node thread pool works

Asked

Viewed 717 times

3

Assuming that Javascript, both in browser and Node, is single thread (correct me if I’m wrong), like thread pool works?

If, for now, my server does not receive many requests or is in development (it only receives my test requests), it makes sense to decrease the UV_THREADPOOL_SIZE?

If my server is receiving many requests there is some gain increase the UV_THREADPOOL_SIZE?

In the case of a server that has small and large processes

  • There is an untruth in this question, there is the possibility of creating multiple "threads" in js. see: 1,2

  • Works identical to any other resource pool scheme. Similar to RunnerPool (or any similar Java name)

  • @Marceloboni print my or the two links are equal?

  • @Jeffersonquesado thanks, can tell me about the use of UV_THREADPOOL_SIZE?

  • @Guilhermecostamilam http://docs.libuv.org/en/latest/threadpool.html studying to give a more assertive answer

  • I was wrong to paste the second link 2*

  • @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

  • 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.

Show 3 more comments

1 answer

3


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.