代码之家  ›  专栏  ›  技术社区  ›  xxCodexx

在Python中从父函数启动进程

  •  0
  • xxCodexx  · 技术社区  · 7 年前

    我想知道我的进程是否已从启动此进程的函数停止工作

    from multiprocessing import Process
    
    def parent_function():
        i = 0
        while True:
            #some code processing
            if i == 0: #start this process only first time
               temp = Process(target=process_function)
               temp.start()
    
    def process_function():
        While True:
            #some code waiting or processing
    

    如果在中发生异常 process_function() 我的 parent_function 不知道这件事。如何从重新启动该流程功能 parent_function() 并不断检查 process\u函数() 是否正在运行。

    1 回复  |  直到 7 年前
        1
  •  1
  •   arudzinska    7 年前

    您可以使用 is_alive() 方法检查子进程是否终止。它返回 True False 。在你的 parent_function() :

    if not temp.is_alive():
        # restart the process