Python: How to determine subprocess children have all finished running

后端 未结 4 1124
难免孤独
难免孤独 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条回答
  •  Happy的楠姐
    2021-02-10 08:10

    You can just use os.waitpid with the the pid set to -1, this will wait for all the subprocess of the current process until they finish:

    import os
    import sys
    import subprocess
    
    
    proc = subprocess.Popen([sys.executable,
                             '-c',
                             'import subprocess;'
                             'subprocess.Popen("sleep 5", shell=True).wait()'])
    
    pid, status = os.waitpid(-1, 0)
    
    print pid, status
    

    This is the result of pstree of different subprocess forked:

    python───python───sh───sleep
    

    Hope this can help :)

提交回复
热议问题