最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run Node Cluster on windows? - Stack Overflow

programmeradmin0浏览0评论

Anyone know how to run Node Cluster on windows? I haven't been able to find any articles on the web and cannot seem to solve this problem:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: write ENOTSUP
    at exports._errnoException (util.js:1007:11)
    at ChildProcess.target._send (internal/child_process.js:634:20)
    at ChildProcess.target.send (internal/child_process.js:521:19)
    at sendHelper (cluster.js:751:15)
    at send (cluster.js:534:12)
    at cluster.js:509:7
    at SharedHandle.add (cluster.js:99:3)
    at queryServer (cluster.js:501:12)
    at Worker.onmessage (cluster.js:449:7)
    at ChildProcess.<anonymous> (cluster.js:765:8)

And the code...

if (cluster.isMaster) {
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
    cluster.on('online', (worker) => {
        console.log('Worker ' + worker.process.pid + ' is online');
    });
    cluster.on('exit', (worker, code, signal) => {
        console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
    });
} else {
    console.log('else part ');
    openPort();
}

function openPort() {
    let server = dgram.createSocket('udp4');
    server.bind(port, host);
    server.on('message', processMessage);
}

Anyone know how to run Node Cluster on windows? I haven't been able to find any articles on the web and cannot seem to solve this problem:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: write ENOTSUP
    at exports._errnoException (util.js:1007:11)
    at ChildProcess.target._send (internal/child_process.js:634:20)
    at ChildProcess.target.send (internal/child_process.js:521:19)
    at sendHelper (cluster.js:751:15)
    at send (cluster.js:534:12)
    at cluster.js:509:7
    at SharedHandle.add (cluster.js:99:3)
    at queryServer (cluster.js:501:12)
    at Worker.onmessage (cluster.js:449:7)
    at ChildProcess.<anonymous> (cluster.js:765:8)

And the code...

if (cluster.isMaster) {
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
    cluster.on('online', (worker) => {
        console.log('Worker ' + worker.process.pid + ' is online');
    });
    cluster.on('exit', (worker, code, signal) => {
        console.log(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
    });
} else {
    console.log('else part ');
    openPort();
}

function openPort() {
    let server = dgram.createSocket('udp4');
    server.bind(port, host);
    server.on('message', processMessage);
}
Share Improve this question edited Sep 1, 2016 at 15:35 wayofthefuture asked Aug 31, 2016 at 15:30 wayofthefuturewayofthefuture 9,4557 gold badges38 silver badges55 bronze badges 7
  • This code works for me - where is the rest of your code and what is your Node.js version. – AntonB Commented Aug 31, 2016 at 15:37
  • What version of node.js are you running. This bug report suggests a bug on Windows in this area was fixed last year. I'm not sure what versions the fix was put into. – jfriend00 Commented Aug 31, 2016 at 17:09
  • Also, what does openPort() do? – jfriend00 Commented Aug 31, 2016 at 17:10
  • It looks like a root cause of this type of error when using clustering on Windows is use of a UDP socket in your clustering. Do you know if you're doing that or using a library that uses UDP? If so, it appears that there are some work-arounds by binding the UDP socket in an appropriate way. If you read this discussion, you get a lot more detail. – jfriend00 Commented Aug 31, 2016 at 17:20
  • Sorry for the delay in response. I edited the code to include the openPort function. Yes it is UDP. Also the Node version is 6.2.2. It's more important it works on Linux for production, wonder if there is a way to be patible with both? @jfriend00 That link seems like they fixed the problem? Thanks. – wayofthefuture Commented Sep 1, 2016 at 15:40
 |  Show 2 more ments

3 Answers 3

Reset to default 2

Support for UDP clustering was added in v0.11.14 (for Linux and OSX). Check file on node.js master, which says "dgram clustering is currently not supported on windows"

In the current node js version I am using below code to create cluster on windows.

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
  // 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");
    cluster.fork();
  });
} else {
  var express = require('express');
  var http = require('http');

  // init app
  var app = express();

  function createServer(app) {
    return http.createServer(app);
  }

  app.locals.server = createServer(app);

  app.locals.server.listen(port, function() {
    console.info("server online");
  });
}

This will create clusters on same port.

So, in order to use UDP with Node cluster on Windows, you have to call server.bind like this:

server.bind({port: 1900, exclusive: true}, function () {
        console.log('PORT BIND SUCCESS');
        server.setBroadcast(true);
        server.setMulticastTTL(128);
        server.addMembership(multicastAddress, myIp);
    });

The key part is to pass in the object {port: PORT, exclusive: true} to the bind function. I found the answer here: https://github./misterdjules/node/mit/1a87a95d3d7ccc67fd74145c6f6714186e56f571

发布评论

评论列表(0)

  1. 暂无评论