每次运行cluster.fork()时,都会出现错误:bind EADDRINUSE

时间:2015-09-12 00:25:51

标签: node.js master-slave

我使用node.js,并使用群集模块。每次我运行cluster.fork()时,我总是得到一个

throw er; // Unhandled 'error' event
Error: bind EADDRINUSE
    at exports._errnoException (util.js:746:11)
at cb (net.js:1205:33)
at rr (cluster.js:592:14)
at Worker.<anonymous> (cluster.js:563:9)
at process.<anonymous> (cluster.js:692:8)
at process.emit (events.js:129:20)
at handleMessage (child_process.js:324:10)
at Pipe.channel.onread (child_process.js:352:11)

我一直在谷歌搜索,我不知道这是怎么回事,因为我没有传递任何端口号码。

由于

编辑:发布代码

var setupWorkers = function() {
   if (cluster.isMaster) {
   // Fork workers.
       for (var i = 0; i < 5; i++) {
       cluster.fork();
   }

 }

这是一个在app.js中调用的函数,我通过调用节点app.js来运行

2 个答案:

答案 0 :(得分:1)

我使用所有线程多次启动服务器,因此端口已经绑定

答案 1 :(得分:0)

您提供的堆栈跟踪表明EADDRINUSE来自net模块。 EADDRINUSE通常意味着您尝试多次侦听IP /端口组合。因此,例如,如果这是一个集群Web服务器,也许所有工作者都试图绑定到同一IP地址上的端口80。如果没有更多代码,就不可能分辨出发生了什么。

您在后续评论中提供的示例代码不会为我触发EADDRINUSE。相反,它与cluster.fork is not a function有误,因为在调用cluster.isMaster之前没有检查cluster.fork()

相关问题