在节点js 8中打印libuv线程池大小 [英] print libuv threadpool size in node js 8
问题描述
此链接纯粹表明libuv
提供了一个线程池,可用于运行用户代码并在循环线程中得到通知.其默认大小为4,但是可以在启动时通过将UV_THREADPOOL_SIZE
环境变量设置为任何值来更改它. (绝对最大值为128).
This link purely specifies that libuv
provides a thread pool which can be used to run user code and get notified in the loop thread. Its default size is 4, but it can be changed at startup time by setting the UV_THREADPOOL_SIZE
environment variable to any value. (the absolute maximum is 128).
因此,在package.json
中,我将scripts
字段设置如下(注意:我正在使用Windows 7,Node JS 8.11.3,nodemon,express 4.16),
So, in package.json
, I set scripts
field as below (NOTE: I am using Windows 7, Node JS 8.11.3, nodemon, express 4.16),
package.json
.
.
.
"scripts": {
"start": "SET UV_THREADPOOL_SIZE = 120 && node index.js",
},
.
.
.
index.js
的代码
var express = require('express');
var app = express();
var server = app.listen(3000, function(){
console.log('LIBUV Threads: ', process.env.UV_THREADPOOL_SIZE); // this returns 'undefined'
});
如何确保设置了线程池大小?我想如上所述在index.js
中将其打印出来.
How can I be assured that thread pool size is set? I want to print it out here in index.js
as above.
推荐答案
set
命令中不应包含空格.
You shouldn't have spaces in your set
command.
set UV_THREADPOOL_SIZE=120 && node index.js
此外,您还应该通过调用start
脚本来启动Node.js程序:
Also you should start your Node.js program by invoking the start
script:
npm start
否则,将不会设置环境变量,并且在代码中访问它时,您将继续获取undefined
.
Otherwise the environmental variable won't be set and you will continue to get undefined
when accessing it in your code.
如果使用的是Nodemon,则可以通过运行带有附加参数的命令来确保调用npm脚本:
If you are using Nodemon, you can ensure your npm script is invoked by running the command with an extra argument:
nodemon --exec npm start
这篇关于在节点js 8中打印libuv线程池大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!