1

Suppose I have an application that needs to run child processes forever, and would only exit on an error. (Say processes are named Q1 and Q2).

Q1 and Q2 are supposed to run forever. However, they may get terminated on an Error. In this case, the main process should restart the terminated process only.

How could I look for any terminated processes and restart them, without any blocking?

Other references 1.

adam
  • 368
  • 2
  • 10
  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 05 '21 at 16:01

1 Answers1

2

You could do something like this:

from multiprocessing import Pool
from time import sleep

def foo():
    print("foo here")
    sleep(3)
    raise Exception

def bar():
    print("bar here")
    while True:
        sleep(5)
        print("'bar'-process still alive")

if __name__ == "__main__":
    restarts = foo, bar
    results = {func: None for func in restarts}
    with Pool(processes=2) as p:
        while True:
            for func in restarts:
                results[func] = p.apply_async(func)
                print(f"'{func.__name__}'-process (re)started")
            sleep(1)
            restarts = (
                func
                for func, result in results.items()
                if result.ready() and not result.successful()
            )
Timus
  • 10,974
  • 5
  • 14
  • 28
  • 1
    Thank you, this seems to work exactly how I need it to. I have a follow up question though, there's a `func` on line 25. I do not understand it's use, could you please explain it. – adam Sep 03 '21 at 07:34
  • 1
    Upon reimplementing the code, I see how it is used and understand my previous question. Please disregard it. Thanks! – adam Sep 03 '21 at 08:20
  • @adam Thanks for the feedback. (I probably should have used a simple loop instead of a comprehension.) – Timus Sep 03 '21 at 08:40