[英]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: 在阅读了关于Node的线程性质的这个很好的答案后,我开始使用UV_THREADPOOL_SIZE
系统变量来改变线程池的大小,我发现了一些有趣的东西:
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). 我的Node进程中有15个线程(我认为它应该是10 + 1主节点线程= 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. 如您所见,有15个线程正在运行。
If I set UV_THREADPOOL_SIZE = 1
, I get 6 threads. 如果我设置UV_THREADPOOL_SIZE = 1
,我得到6个线程。
If I comment out the readFile
line (so the thread pool is not initialized), I get 5 threads. 如果我注释掉readFile
行(所以线程池没有初始化),我得到5个线程。
So I make a conclusion that Node at startup creates 5 threads. 所以我得出结论,启动时Node创建了5个线程。 Why not 1? 为什么不是1?
Can somebody shed some light on this? 有人可以对此有所了解吗?
Edit: I'm using brand new Node 4.0.0 编辑:我正在使用全新的Node 4.0.0
UPDATE: Since node v6.0.0 you can define how many threads are used by V8 via the --v8-pool-size
flag: 更新:从节点v6.0.0开始,您可以通过--v8-pool-size
标志定义V8使用的线程--v8-pool-size
:
--v8-pool-size=num --v8池大小= NUM
Set V8's thread pool size which will be used to allocate background jobs. 设置V8的线程池大小,该大小将用于分配后台作业。 If set to 0 then V8 will choose an appropriate size of the thread pool based on the number of online processors. 如果设置为0,则V8将根据在线处理器的数量选择适当大小的线程池。 If the value provided is larger than V8's maximum, then the largest value will be chosen. 如果提供的值大于V8的最大值,则将选择最大值。
4 extra threads are for use by V8 . 4个额外的线程供V8使用 。 V8 uses these threads to perform various tasks, such as GC-related background tasks and optimizing compiler tasks. V8使用这些线程执行各种任务,例如与GC相关的后台任务和优化编译器任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.