Python 多处理重生崩溃的进程

发布于 2024-10-15 05:42:16 字数 200 浏览 1 评论 0原文

我想创建一些工作进程,如果它们因异常而崩溃,我希望它们重新生成。除了多处理模块中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫轩蝶泪 2024-10-22 05:42:16

轮询以查看子进程是否处于活动状态应该可以正常工作,因为这是一项低开销检查,并且您不需要经常检查。

这个(类似)问题的第一个答案有一个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

油焖大侠 2024-10-22 05:42:16

您可以将工作进程包装在 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.

晨与橙与城 2024-10-22 05:42:16

如果您使用的是类 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(), especially SIGCHLD. I'm afraid I don't remember whether Windows covers SIGCHLD with its very limited POSIX signal support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文