![](/img/trans.png)
[英]How to end the sub process when using subprocess.Popen with python?
[英]return the end of process with subprocess in Python
我正在使用子进程(Python 2.7)来调用软件容器(Docker)上的进程。 此过程会在目录中创建一些文件。 我需要在进程结束并创建文件时,函数 F 返回一个布尔值,以便我知道我可以访问这些文件。 我怎样才能做到这一点?
def F():
cmd1 = 'my_command_that_invoke_docker'
process = subprocess.Popen(cmd1, shell=True)
您可以等待子进程终止。
如果您想要来自子进程的 STDOUT 和/或 STDERR 流(和/或将数据发送到进程的 STDIN),请使用process.communicate
方法:
stdout, stderr = process.communicate()
process.returncode
将具有退出状态。
另一方面,如果您只需要等待进程终止,而不进行任何流捕获,则使用process.wait
方法:
returncode = process.wait()
returncode
将包含子returncode
的退出状态。
** 请注意,在shell=True
情况下,子进程将是 shell 本身(除非您直接exec
任何命令)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.