CNode

Clustor模块子进程的事件触发方式?

问答
Llaoqiren发布于10 年前最后回复10 年前0 回复3531 浏览0 收藏

官方文档关于子进程事件说明中有:

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);

谢谢

查看回复

回复 (0)

暂无回复,成为第一个参与讨论的人。
参与回复
登录后即可参与回复。登录