简体   繁体   English

有没有办法在 node js 的工作线程中使用 parentPort.on() 和 server.listen()

[英]Is there any way to use parentPort.on() in worker threads in node js with server.listen()

Whenever I try to use a worker having每当我尝试使用具有
Worker file =>工人档案 =>

parentPort.on('message',msg =>{
    //some code here
})

Main file =>主文件 =>

server.listen(3000,function(){
    console.log('Server running');
});

with a server already listening it gives this error服务器已经在监听它给出了这个错误

Error: listen EADDRINUSE: address already in use 127.0.0.1:3000
    at Server.setupListenHandle [as _listen2] (net.js:1314:16)
    at listenInCluster (net.js:1362:12)
    at GetAddrInfoReqWrap.doListen [as callback] (net.js:1499:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:68:8)
Emitted 'error' event on Worker instance at:
    at Worker.[kOnErrorMessage] (internal/worker.js:233:10)
    at Worker.[kOnMessage] (internal/worker.js:243:37)
    at MessagePort.<anonymous> (internal/worker.js:164:57)
    at MessagePort.emit (events.js:315:20)
    at MessagePort.onmessage (internal/worker/io.js:78:8)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:11:10) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '127.0.0.1',
  port: 3000
}

I have tried using MessageChannel() but it also gives same error.我试过使用 MessageChannel() 但它也给出了同样的错误。 Could you please tell what is the cause of error and how to correct it.请您告诉错误的原因是什么以及如何纠正它。

EADDERINUSE sounds like you're trying to start a new server on the same port in both the main file and in your worker thread(s). EADDERINUSE 听起来您正在尝试在主文件和工作线程中的同一端口上启动新服务器。

You need to make sure you're not trying to start a server in the worker thread(s) or if you wanted a server in the worker threads, then they each have to be on a unique port.您需要确保您没有尝试在工作线程中启动服务器,或者如果您想要工作线程中的服务器,那么它们每个都必须位于唯一的端口上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM