在 fork 多个子进程的 NodeJS 应用程序中,我需要知道子进程何时退出,问题是 EventEmitter 调用的回调函数没有事件的发送者。
我做过很多 .NET 开发,他们的事件总是有 sender
参数和 arguments
参数。
我如何知道哪个子进程发送了事件?
我可以做类似的事情
const childProcess = fork(...)
childProcess.on('退出', 函数(代码, 信号) {
myCallback(childProcess, 代码, 信号);
}
但是我将无法从该特定事件处理程序中取消注册。
最佳答案
答案是this
将被设置为引用发件人。
来源https://nodejs.org/api/events.html
The eventEmitter.emit() method allows an arbitrary set of arguments to be passed to the listener functions. It is important to keep in mind that when an ordinary listener function is called by the EventEmitter, the standard this keyword is intentionally set to reference the EventEmitter to which the listener is attached.
关于node.js - NodeJS EventEmitter 中事件的发送者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46759562/