python - 如何确保子进程不会在父进程终止时终止?

标签 python ubuntu parent-child python-multiprocessing child-process

我有一个在类中运行子进程的父进程。子进程需要更长的时间才能完成。我想确保父进程终止时子进程不会终止。我怎么做?

这是我的代码的一个非常简化的版本:

# myClass.py
from multiprocess import Process
class myClass(self):
    def __init__(self):
        print ('setup the object')
    def parentProcess(self, idx)
        p = Process(target=childFunc)
        p.start()
        time.sleep(3)
        print ('parent is done with ' + str(idx))
    def childProcess(self):
        print ('do some childish stuff')
        time.sleep(8)

这就是我运行父进程的方式
# main.py
from multiprocessing import Process
myClass import myClass
myC = myClass()
for i in range(10):
    p = Process(target=myC.parentProcess, args=i)
    p.start()
    p.join()

最佳答案

在所有进程完成之前,您的程序不会终止。尝试这个:

from multiprocessing import Process

import time


def foo():
    time.sleep(2)
    print("Now I am done")


if __name__ == "__main__":
    p = Process(target=foo)
    p.start()
    print("I am done.")

但是,要控制流程的执行:
  • 使用child.join()等待子进程结束。
  • 您应该使用两个循环,一个用于启动进程,一个用于加入它们

  • 尝试这个:
    from multiprocessing import Process
    
    import time
    
    
    class MyClass():
        def __init__(self, idx):
            self.idx = idx
    
        def start_parent(self):
            p = Process(target=self.child_func)
            p.start()
            time.sleep(1)
            print('parent is done, waiting for child', self.idx)
            p.join()
            print('parent exiting', self.idx)
    
        def child_func(self):
            print('child start', self.idx)
            time.sleep(2)
            print('child end', self.idx)
    
    if __name__ == "__main__":
        parents = []
        for i in range(10):
            o = MyClass(i)
            p = Process(target=o.start_parent)
            parents.append(p)
            p.start()
    
        for p in parents:
            p.join()
    
        print("all done")
    

    或者更好,子类Process并实现run() :
    from multiprocessing import Process
    
    import time
    
    
    class ParentProcess(Process):
        def __init__(self, idx, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.idx = idx
    
        def run(self):
            print('parent start', self.idx)
            child = ChildProcess(self)
            child.start()
            time.sleep(1)
            print('waiting for child', self.idx)
            child.join()
            print('parent end', self.idx)
    
    
    class ChildProcess(Process):
        def __init__(self, parent, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.parent = parent
    
        def run(self):
            print('child start', self.parent.idx)
            time.sleep(5)
            print('child end', self.parent.idx)
    
    
    if __name__ == "__main__":
        parents = [ParentProcess(i) for i in range(10)]
        for p in parents:
            p.start()
    
        for p in parents:
            p.join()
    
        print("all done")
    

    关于python - 如何确保子进程不会在父进程终止时终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49226394/

    相关文章:

    python - Python 2.6 的 Str.format() 给出错误,而 2.7 没有

    Python, Pandas 将列一分为二

    python - 无法使用 Scrapy dist-packages/cryptography/hazmat/bindings/_openssl.so 启动项目

    javascript - 浏览器中JS字符集错误

    android - 在android中获取 subview 的索引

    angular - 如何使用 Angular 选择器将一个组件添加到另一个组件中?

    python - 在 Python 3 中使用 Mutagen 将专辑封面嵌入到 mp3

    ubuntu - 菜单未出现在 VSCode 应用程序中 [ed] 0.10.3 Linux Ubuntu 15.10

    ubuntu - 如何通过ansible playbook评论多行

    react-native - 在 React Native 中将状态从子组件传递到父组件