![](/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.