阅读了这个很棒的答案关于Node.js线程的性质后,我开始尝试使用UV_THREADPOOL_SIZE
系统变量来改变线程池的大小,然后我发现了一些有趣的事情:
当我设置
process.env.UV_THREADPOOL_SIZE = 10;
我的Node进程中有15个线程(我认为应该是10个加上1个主Node线程=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);
运行后我键入:
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在启动时创建了5个线程。 为什么不是1个?
能否有人解释一下这个问题?
编辑:我正在使用全新的Node 4.0.0版本。