how to restart subprocess if it crashes?

I'm trying to restart a subprocess if it crashes, but somewhy this loop just doesn't work. I've been wondering if that's even possible?

def dont_stop(conv):
    try:['python', '', str(conv)])

if __name__ == '__main__':
    proc = []
    for conv in range(3,8):
        p = multiprocessing.Process(name=f'p{conv}', target=dont_stop, args=(conv,))
    for p in proc:

1 answer

  • answered 2022-05-06 21:23 Blckknght

    The function doesn't raise an exception if the program it is running exits in a non-standard way. All it does is return the "return code" from the process you told it to run. That's usually 0 for a process that exits normally, and some other value for a program that crashes (the specific meanings of non-zero values vary between programs and OSs).

    Here's a simple solution that replaces your recursive code with a loop that checks the return value of the subprocess:

    def dont_stop(conv):
        retval = 1
        while retval != 0:      # a return value of zero indicates a normal exit
            retval =['python', '', str(conv)])

    An alternative approach is to stop using and use subprocess.check_call instead. That function checks the return code and raises an exception if it's not zero. While often that's what we'd prefer, it's actually a bit uglier here.

    def dont_stop(conv):
        while True:
                subprocess.check_call(['python', '', str(conv)])
            except subprocess.CalledProcessError:
                # do logging here?

    Since the program you're running is also a Python program, you might consider importing it, rather than running it in a separate interpreter. That might let your dont_stop function directly interact with the code, such as catching and logging exceptions. The details of that are much too dependent on the design of and what it's supposed to be doing though, so I'm not going to show any suggested code for this approach.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum