首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Node.js cluster.fork()没有在Windows8上创建我的进程的副本?

问为什么Node.js cluster.fork()没有在Windows8上创建我的进程的副本?
EN

Stack Overflow用户
提问于 2014-02-27 15:14:59
回答 1查看 1.2K关注 0票数 4

我正在试验Node.js,看看它在我的公司里是否可行。作为一个初学者,我试图在没有Express的情况下处理Node (或者是其他选择),事实上,一个未处理的异常会导致整个过程关闭,基本上让我脱离网络,这似乎是一个问题。它非常清楚地说明了not to use an uncaught exception handler对于进程吞噬错误的作用。

如果我把它和domains结合起来,Node.js的cluster module似乎可以很好地处理这个问题。事实上,domains recommend using them to just send out a 500和使用集群,以便您可以实际关闭出错的进程。

我运行的是Windows8EnterpriseMSI,我的操作系统/体系结构使用的是installed Node.js。我已经让Node.js工作了,并且一直在使用它。但是,当我从cluster module tutorial运行示例代码,然后尝试访问http://localhost:8000时,我的浏览器无法连接。如果我在else块中放入一个console.log()调用,它也永远不会命中。

下面是我使用的代码:

代码语言:javascript
复制
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    // Tried this using what the defaults are from http://nodejs.org/api/cluster.html#cluster_cluster_settings
    // The values of these are undefined otherwise. Seems to make no difference.
    // Doesn't work if I remove this line either.
    cluster.settings = {
        execArgv: process.execArgv,
        exec: process.argv[1],
        args: process.argv.slice(2),
        silent: false
    };

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

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
    console.log("This never hits.");
    http.createServer(function(req, res) {
        res.writeHead(200);
        res.end("hello world\n");
    }).listen(8000);
}

这是我可以在Windows8企业版x64上解决的问题吗?有没有关于它为什么不工作的解释?

EN

回答 1

Stack Overflow用户

发布于 2017-12-15 20:03:59

在Windows8上使用Visual Studio调试nodejs应用程序时,我偶然发现了这个问题。

检查周围是否有可能阻塞侦听端口的陈旧nodejs进程。令人惊讶的是,当端口被另一个进程阻塞时,listen()没有异常。在我的案例中,我手动终止了陈旧的进程,集群按预期工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22061569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档