Python: How to determine subprocess children have all finished running

后端 未结 4 1111
难免孤独
难免孤独 2021-02-10 07:14

I am trying to detect when an installation program finishes executing from within a Python script. Specifically, the application is the Oracle 10gR2 Database. Currently I am usi

4条回答
  •  伪装坚强ぢ
    2021-02-10 07:47

    Ok, here is a trick that will work only under Unix. It is similar to one of the answers to this question: Ensuring subprocesses are dead on exiting Python program. The idea is to create a new process group. You can then wait for all processes in the group to terminate.

    pid = os.fork()
    if pid == 0:
        os.setpgrp()
        oracle_subprocess = subprocess.Popen(OUI_DATABASE_10GR2_SUBPROCESS)
        oracle_subprocess.wait()
        os._exit(0)
    else:
        os.waitpid(-pid)
    

    I have not tested this. It creates an extra subprocess to be the leader of the process group, but avoiding that is (I think) quite a bit more complicated.

    I found this web page to be helpful as well. http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/

提交回复
热议问题