Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

child_process.fork会提示端口占用 cluster.fork就不会这是为什么

$
0
0

server.js

var http = require('http');
const PORT=8888;
var server = http.createServer(function (request, response) {
 response.end ("pid:"+process.pid);
});
server.on("listening",function(){

  console.log('process ',process.pid,"listen at ",PORT);
});
server.listen(PORT);


用cluster启动四个 Server.js进程

var cluster = require('cluster');
if (cluster.isMaster) {
  require('os').cpus().forEach(function(){
    cluster.fork();
  });

}else {

require('./server.js');

}


http://localhost:8888访问正常

用child_process 启用4个 server.js进程

const cp = require('child_process');

require('os').cpus().forEach(function(){
    cp.fork("./server.js");
  });

提示Error: listen EADDRINUSE :::8888

这是什么原因?


Viewing all articles
Browse latest Browse all 14821

Trending Articles