def call(*popenargs, timeout=None, **kwargs):
"""Run command with arguments. Wait for command to complete or
timeout, then return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
with Popen(*popenargs, **kwargs) as p:
try:
return p.wait(timeout=timeout)
except: # Including KeyboardInterrupt, wait handled that.
p.kill()
# We don't call p.wait() again as p.__exit__ does that for us.
raise
当使用
subprocess.call
时,您不能这样做,因为它在运行子进程时会中断您的程序如何终止使用
subprocess.Popen
创建的子进程在this question中给出了答案你必须离开
subprocess.call
去做这件事,但它仍然会达到同样的效果。子流程调用运行传递的命令,等待完成,然后返回returncode属性。在我的示例中,我展示了如果需要,如何在完成时获取returncode下面是一个如何终止子进程的示例,您必须截获SIGINT(Ctrl+c)信号,以便在退出主进程之前终止子进程。如果需要,还可以从子流程获取标准输出、标准错误和returncode属性
您想使用
Popen
。下面是subprocess.call
的样子:subprocess.call
专门用于在返回之前等待进程完成。所以,当subprocess.call
结束时,没有什么可杀的了如果您想启动一个子流程,然后在它运行时执行其他操作,包括终止该流程,那么应该直接使用
subprocess.Popen
相关问题 更多 >
编程相关推荐