Python多进程 - 监视进程并在失败时重启

16 投票
1 回答
17341 浏览
提问于 2025-04-17 20:29

考虑一下这段有效的代码:

from multiprocessing import Process
from updaters import app1, app2

if __name__ == '__main__':

    apps = [ app1, app2]  
    for app in apps:
        instance = app()
        p = Process(target=instance.start_listener)
        p.start()
        p.join()

这段代码运行得很好,直到有一个进程出现问题(比如说发生了某种异常)——我该如何编写程序来监控这个进程,并在它失败时重新启动它呢?

1 个回答

21

定期检查一下进程的状态,如果它返回的是假(False),那就删除这个进程,然后重新启动一个新的进程,比如:

from multiprocessing import Process
from updaters import app1, app2
from time import sleep

if __name__ == '__main__':

    apps = [app1, app2]  
    processes = {}
    n = 0
    for app in apps:
        instance = app()
        p = Process(target=instance.start_listener)
        p.start()
        processes[n] = (p, app) # Keep the process and the app to monitor or restart
        n += 1

    while len(processes) > 0:
        for n in processes.keys():
            (p, a) = processes[n]
            sleep(0.5)
            if p.exitcode is None and not p.is_alive(): # Not finished and not running
                 # Do your error handling and restarting here assigning the new process to processes[n]
                 print(a, 'is gone as if never born!')
            elif p.exitcode < 0:
                print ('Process Ended with an error or a terminate', a)
                # Handle this either by restarting or delete the entry so it is removed from list as for else
            else:
                print (a, 'finished')
                p.join() # Allow tidyup
                del processes[n] # Removed finished items from the dictionary 
                # When none are left then loop will end
print ('FINISHED')

撰写回答