UV_THREADPOOL_SIZE如何工作?

时间:2017-02-08 15:24:28

标签: node.js

我正在使用node.js集群和一个使用集群模块创建的工作进程,并将UV_THREADPOOL_SIZ E修复为1.但是当我使用时:

ps -eLf | grep node  

我越来越多的线程了。这是ps命令输出的片段:

root      3803  2027  3803  0    6 14:58 pts/0    00:00:00 node-v6.9.4-linux-x64/bin/node cluster.js 
root      3803  2027  3804  0    6 14:58 pts/0    00:00:00 node-v6.9.4-linux-x64/bin/node cluster.js 
root      3803  2027  3805  0    6 14:58 pts/0    00:00:00 node-v6.9.4-linux-x64/bin/node cluster.js 
root      3803  2027  3806  0    6 14:58 pts/0    00:00:00 node-v6.9.4-linux-x64/bin/node cluster.js 
root      3803  2027  3807  0    6 14:58 pts/0    00:00:00 node-v6.9.4-linux-x64/bin/node cluster.js
root      3803  2027  3808  0    6 14:58 pts/0    00:00:00 node-v6.9.4-linux-x64/bin/node cluster.js 
root      3809  3803  3809  0    7 14:58 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js 
root      3809  3803  3810  0    7 14:58 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js 
root      3809  3803  3811  0    7 14:58 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js 
root      3809  3803  3812  0    7 14:58 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js 
root      3809  3803  3813  0    7 14:58 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js 
root      3809  3803  3814  0    7 14:58 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js
root      3809  3803  3833  0    7 14:59 pts/0    00:00:00 /root/node-v6.9.4-linux-x64/bin/node /root/cluster.js

这是node.js代码:

Const cluster = require('cluster'); 
var http = require('http'), fs = require('fs'); 
process.env.UV_THREADPOOL_SIZE = 1;
const numCPUs = 1

if (cluster.isMaster) {<br>
  console.log(`Master ${process.pid} is running`);

  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  // Workers can share any TCP connection
  // In this case it is an HTTP server
  http.createServer((request, response) => {
    if (request.url == "/page.html" || request.url == "/") {
    response.writeHeader(200, {"Content-Type": "text/html"});
    fs.createReadStream("./page.html").pipe(response);
  }
  else if (request.url == "/index.js") {
    response.writeHeader(200, {"Content-Type": "text/html"});
    fs.createReadStream("./page.html").pipe(response);
  }
  }).listen(8000);

  console.log(`Worker ${process.pid} started`);
}

0 个答案:

没有答案