我对使用Cluster的Visual Studio 代码有一些问题
编辑
如果我按 Ctrl + F5它可以正常工作,除了F5之外它还在做什么,我是否需要始终使用 Ctrl 启动命令?
---
当使用 VS Code Launch 命令(F5)启动时,似乎工作人员永远不会启动。我是否需要对 .vscode/launch.json 文件进行一些更改以使集群正常工作。
实际代码复制自 Node.js 6 api https://nodejs.org/api/cluster.html#cluster_cluster
npm test Windows 命令提示符显示:
Master started
Listening port 80
Listening port 80
Listening port 80
Listening port 80
VS Code (F5)调试控制台显示:
node --debug-brk=7601 --nolazy index.js
Debugger listening on port 7601
Master started
Debugger listening on port 7602
Debugger listening on port 7603
Debugger listening on port 7604
Debugger listening on port 7605
VS 代码启动.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
..........
index.js
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
console.log('Master started')
for (var 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((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(80);
console.log('Listening port 80')
}