20

One of the changes made by Ben Noordhius in Node v0.10.0 was to "honor UV_THREADPOOL_SIZE environment variable" in Unix. The "threadpool.c" source file seems to do just that.

If I don't set this env variable, I can verify that I am limited to a threadpool of 4 threads, which is the default size of the threadpool.

But I set this environment variable on my Linux server to 64 and then restart Node, but I still seem to be limited, seemingly to a threadpool of size of 5?!

Does this make any sense to anyone? Thanks!

user2330260
  • 257
  • 1
  • 2
  • 5
  • If you have less than [insert your target thread pool size here] cpu cores, beware: http://stackoverflow.com/q/1718465/149444 – Ricardo Stuven Apr 24 '15 at 19:39
  • @RicardoStuven additional threads beyond 1 per core will still help in Node, since the libuv threadpool is generally only used for IO and not for computation. This is addressed in the first line of [the accepted answer](http://stackoverflow.com/a/1718522/675721). – Codebling Dec 09 '16 at 04:40
  • can someone answer this as it is related to above question https://stackoverflow.com/q/50552693/3296607 – Ankur Soni May 27 '18 at 13:47

5 Answers5

18

It seems that you must set it the var with node command or from inside the node program. Execute it like:

UV_THREADPOOL_SIZE=64 node

or modify from program :

process.env.UV_THREADPOOL_SIZE=64
//then execute some function that requires threadpool
require('fs').readFile('testing',function(){});

Testing threads:

ps -Lef | grep  "\<node\>" | wc -l
67
user568109
  • 47,225
  • 17
  • 99
  • 123
  • I only see 6 threads when I add UV_THREADPOOL_SIZE to process.env. Do you need to readFile to see it? – 4m1r Aug 12 '15 at 22:06
  • 2
    Putting `export UV_THREADPOOL_SIZE=64` in your `.bashrc` or equivalent will also apply it to any node process started under that shell. – DuBistKomisch Jul 07 '16 at 04:49
5

If you're running a Windows OS and running via a .js file you'll need to set the UV_THREADPOOL_SIZE prior to calling the script via node.

Example in cmd: SET UV_THREADPOOL_SIZE=2 && node my-file-to-run.js (no spaces around the =)

Or in Powershell: $env:UV_THREADPOOL_SIZE = 2 && node my-file-to-run.js

IniasP
  • 125
  • 1
  • 6
mche
  • 616
  • 10
  • 16
  • i think it needs to remove spaces around '=', so it should be SET UV_THREADPOOL_SIZE=2 && node my-file-to-run.js – alkas Aug 11 '19 at 10:06
2

In Windows OS, you need to set the UV_THREADPOOL_SIZE prior to calling the script via node in command line.

e.g. $env:UV_THREADPOOL_SIZE = 5; node script.js

0

Make sure you never set UV_THREADPOOL_SIZE to a number bigger than the amount of core processors you have as this will increase the context switching thus impacting the performance of your app.

const OS = require('os');
process.env.UV_THREADPOOL_SIZE = OS.cpus().length;
Ioannis Suarez
  • 587
  • 6
  • 5
0

The above statement is not correct. libuv is not used if the underlying OS service performs it's own non blocking strategy. If the underlying OS service is blocking, the node implementation will usually wrap the operation as async by using a thread and continuing to execute other JS code on the main thread.

Context switching does not happen because more or less threads are available. The only side effect of having more threads is some ram that is allocated.

Native libraries that can be loaded, usually also make use of libuv. There is a chapter on libuv for instance on the Oracle node driver manual. There it is specifically noted that the Oracle driver performs exchanges with the DB in a libuv thread - so to have multiple connections to the DB open and doing work, it is highly recommended to up process.env.UV_THREADPOOL_SIZE

Please also note of the line of code below, it is totally wrong, you have to set this before node starts, setting it from within node has no effect.

process.env.UV_THREADPOOL_SIZE = OS.cpus().length;
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    On Windows the variable must be set before the process starts. On Linux & macOS we've noticed you can set the variable in the app code but it must be set before the threadpool starts. Since it can be tricky to set in the correct point of execution on Linux & macOS, and you don't get any errors, I agree that it is better to always set the variable before the process starts. – Christopher Jones Jan 14 '22 at 22:20