worker.kill([signal])


  • signal <string> 要发送给工作进程的终止信号名称。默认值: 'SIGTERM'

这个函数会终止工作线程。在主进程中,它通过断开 worker.process 实现,一旦断开,就使用 signal 杀死它。在工作线程中,它通过断开通道实现,然后以代码 0 退出。

【This function will kill the worker. In the primary, it does this by disconnecting the worker.process, and once disconnected, killing with signal. In the worker, it does it by disconnecting the channel, and then exiting with code 0.】

因为 kill() 尝试优雅地断开工作进程,所以它可能会无限期地等待断开完成。例如,如果工作进程进入无限循环,优雅断开将永远不会发生。如果不需要优雅断开行为,请使用 worker.process.kill()

【Because kill() attempts to gracefully disconnect the worker process, it is susceptible to waiting indefinitely for the disconnect to complete. For example, if the worker enters an infinite loop, a graceful disconnect will never occur. If the graceful disconnect behavior is not needed, use worker.process.kill().】

导致 .exitedAfterDisconnect 被设置。

【Causes .exitedAfterDisconnect to be set.】

为了向后兼容,此方法被别名为 worker.destroy()

【This method is aliased as worker.destroy() for backward compatibility.】

在一个 worker 中,process.kill() 存在,但它不是这个函数;它是 kill()

【In a worker, process.kill() exists, but it is not this function; it is kill().】