开发者

Python Multiprocessing respawn crashed processes

开发者 https://www.devze.com 2023-02-07 03:11 出处:网络
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

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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消