Python:等待外部启动的进程完成 [英] Python: waiting for external launched process finish

查看:99
本文介绍了Python:等待外部启动的进程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题中已有的问题 - 如何让 python 脚本等到使用 os.system() 调用启动的某个进程完成?例如像

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

    for i in range( 0, n ):
       os.system( 'someprog.exe %d' % i )

这会同时启动请求的进程 n 次,这可能会让我的电脑有点出汗)

This launches the requested process n times simultaneously, which may make my pc to sweat a bit )

感谢您的建议.

推荐答案

os.system() 确实等待其进程完成后再返回.

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

如果您看到它不等待,则您正在启动的进程可能会自行分离以在后台运行,在这种情况下,Dor 提供的 subprocess.Popen + 等待示例无济于事.

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.

旁注:如果你想要的是 subprocess.Popen + 等待使用 subprocess.call:

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

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

除了显式传递命令和参数而不是将其作为单个字符串传递之外,这与 os.system() 没有什么不同.

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.

这篇关于Python:等待外部启动的进程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆