Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many threads does Node actually create?

After reading this great answer about Node's thread nature, I started to play with UV_THREADPOOL_SIZE system variable to change the size of thread pool, and I found something interesting:

When I set

process.env.UV_THREADPOOL_SIZE = 10; 

I get 15 threads in my Node process (I thought it should be 10 + 1 main Node thread = 11).

Have a look at my script:

process.env.UV_THREADPOOL_SIZE = 10;  //init thread pool by calling `readFile` function require('fs').readFile(__filename, 'utf8', function(err, content) {});  //make node not exiting setInterval(function() {}, 1000); 

After running it I type:

ps -Lef | grep test.js | grep -v grep 

and get the following results:

olegssh   4869  4301  4869  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4870  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4871  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4872  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4873  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4874  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4875  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4876  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4877  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4878  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4879  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4880  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4881  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4882  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js olegssh   4869  4301  4883  0   15 16:38 pts/0    00:00:00 /home/olegssh/node/bin/node test.js 

As you can see there are 15 threads running.

If I set UV_THREADPOOL_SIZE = 1, I get 6 threads.

If I comment out the readFile line (so the thread pool is not initialized), I get 5 threads.

So I make a conclusion that Node at startup creates 5 threads. Why not 1?

Can somebody shed some light on this?

Edit: I'm using brand new Node 4.0.0

like image 526
Oleg Avatar asked Sep 10 '15 15:09

Oleg


People also ask

How many threads does node have?

Conclusion. Node. js has two types of threads: one Event Loop and k Workers.

Is node really single threaded?

No. NodeJs is not single threaded. The NodeJs event loop operates on a single thread yes, but the async blocking operations are delegated to separate worker threads. These threads notify the main thread when they are done processing.

Does node have multiple threads?

You can achieve multithreading by generating multiple nodes or Node. js V8 engines which in isolation are single-threaded. It is still correct to say Node. js is not multi-threaded.

What is the maximum number of threads that can point at a node?

As nodejs works on event loop and it will assign the IO operation to thread pool, but thread pool default size is 4, so at same time maximum 4 thread (IO operation) can work and rest has to wait in queue. Once any thread complete the execution, they can process.


1 Answers

UPDATE: Since node v6.0.0 you can define how many threads are used by V8 via the --v8-pool-size flag:

--v8-pool-size=num
Set V8's thread pool size which will be used to allocate background jobs. If set to 0 then V8 will choose an appropriate size of the thread pool based on the number of online processors. If the value provided is larger than V8's maximum, then the largest value will be chosen.

4 extra threads are for use by V8. V8 uses these threads to perform various tasks, such as GC-related background tasks and optimizing compiler tasks.

like image 187
mscdex Avatar answered Oct 07 '22 21:10

mscdex