4

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.

Alex Amato
  • 1,591
  • 4
  • 19
  • 32

3 Answers3

2

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

Community
  • 1
  • 1
payne
  • 13,833
  • 5
  • 42
  • 49
1

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
1

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.

ʇsәɹoɈ
  • 22,757
  • 7
  • 55
  • 61