Node实际创build了多less个线程?
在阅读了关于Node的线程本质的这个很好的回答之后,我开始使用UV_THREADPOOL_SIZE
系统variables来改变线程池的大小,并且发现了一些有趣的东西:
当我设置
process.env.UV_THREADPOOL_SIZE = 10;
我得到15个线程在我的节点进程(我认为它应该是10 + 1主节点线程= 11)。
看看我的脚本:
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);
运行后,我input:
ps -Lef | grep test.js | grep -v grep
并得到以下结果:
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
正如你所看到的,有15个线程正在运行。
如果我设置UV_THREADPOOL_SIZE = 1
,我得到6个线程。
如果我注释掉readFile
行(所以线程池没有初始化),我得到5个线程。
所以我得出结论,Node在启动时创build了5个线程。 为什么不是1?
有人可以解释一下吗?
编辑:我正在使用全新的节点4.0.0
V8使用 4个额外的线程。 V8使用这些线程来执行各种任务,如GC相关的后台任务和优化编译器任务。