Python: waiting for external launched process finish

前端 未结 2 1430
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 01:26

The question already in title - how can one make the python script wait until some process launched with os.system() call is completed ? For example a code like



        
相关标签:
2条回答
  • 2020-12-03 02:06

    Use subprocess instead:

    import subprocess
    for i in xrange(n):
      p = subprocess.Popen(('someprog.exe', str(i))
      p.wait()
    

    Read more here: http://docs.python.org/library/subprocess.html

    0 讨论(0)
  • 2020-12-03 02:11

    os.system() does wait for its process to complete before returning.

    If you are seeing it not wait, the process you are launching is likely detaching itself to run in the background in which case the subprocess.Popen + wait example Dor gave won't help.

    Side note: If all you want is subprocess.Popen + wait use subprocess.call:

    import subprocess
    subprocess.call(('someprog.exe', str(i)))
    

    That is really no different than os.system() other than explicitly passing the command and arguments in instead of handing it over as a single string.

    0 讨论(0)
提交回复
热议问题