-
Libuv has a default thread pool size of 4, and uses a queue to manage access to the thread pool - the upshot is that if you have 5 long-running DB queries all going at the same time, one of them (and any other asynchronous action that relies on the thread pool) will be waiting for those queries to finish before they even get started.
-
Note, however, that tuning UV_THREADPOOL_SIZE may make more sense for a standalone application like a CLI written in Node.js. If you are standing up a bunch of Node.js processes using the cluster module then I would be surprised if tuning UV_THREADPOOL_SIZE was particularly beneficial for you. But if your application resembles the web tooling benchmarks then tuning UV_THREADPOOL_SIZE may help with performance.
Last active
June 27, 2024 01:35
-
-
Save rjoydip-zz/36031b3b6d46a9d638e3a97816d2a137 to your computer and use it in GitHub Desktop.
How you can set `UV_THREADPOOL_SIZE` value?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {readdir} = require('fs'); | |
process.env.UV_THREADPOOL_SIZE = 6; // This will work | |
readdir('.', () => { | |
process.env.UV_THREADPOOL_SIZE = 20; // This won't | |
}); | |
[1,2,3].forEach(element => { | |
process.env.UV_THREADPOOL_SIZE = 7; // This will work because this isn't a async task | |
}); | |
process.stdout.write("[UV_THREADPOOL_SIZE]", process.env.UV_THREADPOOL_SIZE); | |
// Note: You cannot change the size of the thread pool once it is created or entered in the event-loop/worker-thread. | |
// npm-script: cross-env UV_THREADPOOL_SIZE=5 node index | |
// install "cross-env" as devDependencies |
The code tapas77 posted is from a Node.js course I am also taking (Master Node by building a real-world RESTful API and web app (with authentication, Node.js security, payments & more) by Jonas Schmedtmann). The instructor is running the server on a Mac, which makes it work as is. I've managed to make it work on Windows by installing cross-env (I did it globally), which rjoydip mentioned at the bottom. Setting the variable through cross-env works and you get the expected delay in the console, due to only one thread pool being available.
Awesome, thanks for the update!
…On Mon, 28 Jun 2021, 2:33 pm Repent and Believe the Gospel, < ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
The code tapas77 posted is from a Node.js course I am also taking (Master
Node by building a real-world RESTful API and web app (with authentication,
Node.js security, payments & more) by Jonas Schmedtmann). The instructor is
running the server on a Mac, which makes it work as is. I've managed to
make it work on Windows by installing cross-env (I did it globally), which
rjoydip mentioned at the bottom. Setting the variable through cross-env
works and you get the expected delay in the console, due to only one thread
pool being available.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/36031b3b6d46a9d638e3a97816d2a137#gistcomment-3795204>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOSBJS3PGEHAIOQDH4F6FITTVA3G5ANCNFSM437PEBXA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will get same output that doesn't change your application output. To changing
UV_THREADPOOL_SIZE
values means you resizing thread pool queue. That means you can execute that many numbers (e.g.: If value is 6 then you have 7 queues) of async tasks at the same time.