我在 Visual studio code 和 Cluster 方面遇到一些问题
编辑
如果我按下 Ctrl + F5 它会正常工作,除了 F5 之外它还做了什么,我是否需要始终使用 Ctrl 启动命令?
---
使用 VS Code Launch 命令 (F5) 启动时,worker 似乎永远不会启动。我是否需要对 .vscode/launch.json 文件进行一些更改以使 Cluster 正常工作。
实际代码是从 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 Code launch.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')
}
最佳答案
我遇到了同样的问题。 weinand 在 https://github.com/Microsoft/vscode/issues/3201 中描述的第二个解决方法对我有用:
Launch node from a terminal and attach to it with the VS Code debugger.
Run in terminal: node --debug app.js
Then select the default 'attach' launch config and attach to it.
The workaround is the preferred way if you actually want to debug any worker and not just the first process that is launched.
关于javascript - Node.js v6.0.0 上的 Visual Studio 代码 (F5) 集群问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37012179/