官方文档关于子进程事件说明中有:
A Worker object contains all public information and method about a worker. In the master it can be obtained using cluster.workers. In a worker it can be obtained using cluster.worker.
所以我尝试在分离的worker.js中监听跟Worker相关的事件:
const http = require(‘http’); const cluster = require(‘cluster’); const worker = cluster.worker; http.createServer((req,res)=>{ res.writeHead(200); res.end(“hello world”); }).listen(8099); worker.on(‘listening’,address=>{ console.log(
address:${address}
); });
但是我发现,listening事件回调函数不会执行,难道只能如文档例子那样子master进程代码里监听它们吗?
cluster.fork().on(‘listening’,cb);
谢谢