Node.js Node实际创建了多少线程?
在阅读了节点的线程性质之后, 我开始使用Node.js Node实际创建了多少线程?,node.js,multithreading,threadpool,libuv,Node.js,Multithreading,Threadpool,Libuv,在阅读了节点的线程性质之后, 我开始使用UV\u THREADPOOL\u SIZE系统变量来更改线程池的大小,我发现了一些有趣的东西: 当我设定 process.env.UV_THREADPOOL_SIZE = 10; 我的节点进程中有15个线程(我认为应该是10+1个主节点线程=11) 请看一下我的脚本: process.env.UV_THREADPOOL_SIZE = 10; //init thread pool by calling `readFile` function requi
UV\u THREADPOOL\u SIZE
系统变量来更改线程池的大小,我发现了一些有趣的东西:
当我设定
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);
运行后,我键入:
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\u THREADPOOL\u SIZE=1
,我将得到6个线程
如果注释掉readFile
行(因此线程池未初始化),则得到5个线程
所以我得出一个结论,节点在启动时创建了5个线程。为什么不是1呢
有人能解释一下吗
编辑:我正在使用全新的节点4.0.0更新:自节点6.0.0以来,您可以通过
--V8池大小
标志定义V8使用的线程数:
--v8池大小=num设置用于分配后台作业的V8线程池大小。 如果设置为0,则V8将根据联机处理器的数量选择适当大小的线程池。 如果提供的值大于V8的最大值,则将选择最大值。
需要4个额外的线程。V8使用这些线程执行各种任务,例如与GC相关的后台任务和优化编译器任务。哇,这是一个非常直截了当的答案。非常感谢你!