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:
        subprocess.call(['python', 'main.py', str(conv)])
    except:
        dont_stop(conv)

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

1 answer

  • answered 2022-05-06 21:23 Blckknght

    The subprocess.call 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 = subprocess.call(['python', 'main.py', str(conv)])
            
    

    An alternative approach is to stop using subprocess.call 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:
            try:
                subprocess.check_call(['python', 'main.py', str(conv)])
                break
            except subprocess.CalledProcessError:
                # do logging here?
                pass
    

    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 main.py code, such as catching and logging exceptions. The details of that are much too dependent on the design of main.py 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