Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.env.UV_THREADPOOL_SIZE not working?

I am trying to understand the threadpool in nodeJS. on running the code with creating process.env.UV_THREADPOOL_SIZE = 5;

process.env.UV_THREADPOOL_SIZE = 5;

const https = require('https');
const crypto = require('crypto');
const fs = require('fs');

const start = Date.now()

function doRequest() {
    https.request('https://google.com', res => {
        res.on('data', () => {});
        res.on('end', () => {
            console.log('Request:', Date.now() - start)
        })
    })
    .end()
}
function doHash(){
    crypto.pbkdf2("a", "b", 100000, 512, 'sha512', () => {
        console.log("Hash:", Date.now() - start);
    })
}

doRequest();

fs.readFile('multitask.js', 'utf8', () => {
    console.log('fs:', Date.now() - start)
});

doHash();
doHash();
doHash();
doHash();

I get the output in the terminal:

$ node multitask.js
Request: 641
Hash: 4922
fs: 4925
Hash: 5014
Hash: 5039
Hash: 6512

And after changing the threadpool size to 1: I get the same output.

Request: 501
Hash: 4025
fs: 4028
Hash: 4087
Hash: 4156
Hash: 5079

Could anyone tell me where is the problem exactly?

like image 507
cptiwari20 Avatar asked Jul 07 '18 16:07

cptiwari20


People also ask

What is Uv_threadpool_size?

This thread pool is internally used to run all file system operations, as well as getaddrinfo and getnameinfo requests. Its default size is 4, but it can be changed at startup time by setting the UV_THREADPOOL_SIZE environment variable to any value (the absolute maximum is 1024). Changed in version 1.30.

How do I change the thread pool size in node JS?

To increase the threadpool size, set the environment variable UV_THREADPOOL_SIZE=N up to 1024.


1 Answers

The easiest solution for me was just to add a npm script entry like so:

{
  ...  
  "main": "app.js",
  "scripts": {
     "start": "set UV_THREADPOOL_SIZE=2 & node app.js"
  },
  ...
}

And then, in the cmd:

npm run start
like image 153
graumanoz Avatar answered Sep 28 '22 03:09

graumanoz