Python 多处理重生崩溃的进程
我想创建一些工作进程,如果它们因异常而崩溃,我希望它们重新生成。除了多处理模块中的 is_alive 方法之外,我似乎找不到其他方法来做到这一点。
这将需要我迭代所有正在运行的进程(在睡眠后)并检查它们是否还活着。这本质上是一个繁忙的循环,我想知道是否有更好的解决方案可以在我的任何一个工作进程崩溃时唤醒我的程序。一旦它醒来,我想记录导致程序崩溃的异常并启动另一个进程。
I want to create some worker processes and if they crash due to an exception, I would like them to respawn. Aside from the is_alive method in the multiprocessing module, I can't seem to find a way to do this.
This would require me to iterate over all the running processes (after a sleep) and check if they are alive. This is essentially a busy loop, I was wondering if there was a better solution that will wake up my program in the event that any one of my worker processes has crashed. Once it wakes up, I would like to log th exception that crashed my program and launch another process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
轮询以查看子进程是否处于活动状态应该可以正常工作,因为这是一项低开销检查,并且您不需要经常检查。
这个(类似)问题的第一个答案有一个Python代码示例: 多服务器python 中的监控/自动重启
Polling to see if the child processes are alive should work fine, since it's a low-overhead check and you don't need to check that often.
The first answer to this (similar) question has a Python code example: Multi-server monitor/auto restarter in python
您可以将工作进程包装在 try/ except 块中,其中 except 在引发之前将消息推送到管道上。当然,民意调查并不比这更糟糕,而且更简单。
You can wrap your worker processes in try/except blocks where the except pushes a message onto a pipe before raising. Of course, polling isn't really worse than this and it's simpler.
如果您使用的是类 UNIX 系统,则可以通过安装 来通知您的主程序有关已死亡子进程的信息信号处理程序。查找有关
signal()
的操作系统文档,尤其是SIGCHLD
。恐怕我不记得 Windows 是否以其非常有限的 POSIX 信号支持来覆盖 SIGCHLD。If you're on a unix-like system, your main program can be notified of dead children by installing a signal handler. Look up your operating system's documentation on
signal()
, especiallySIGCHLD
. I'm afraid I don't remember whether Windows covers SIGCHLD with its very limited POSIX signal support.