python - Python os.popen : How to ensure popen(…) has completed execution before continuing?

标签 python multithreading popen

我有以下代码:

pwd = '/home/user/svnexport/Repo/'

updateSVN = "svn up " + pwd
cmd = os.popen(updateSVN)

getAllInfo = "svn info " + pwd + "branches/* " + pwd + "tags/* " + pwd + "trunk/*"
cmd = os.popen(getAllInfo)

cmd = os.popen(updateSVN)开始执行之前,如何确定cmd = os.popen(getAllInfo)已经完成执行?

最佳答案

您应该使用subprocess:

import subprocess
import glob
pwd = '/home/user/svnexport/Repo/'

updateSVN = ["svn", "up", pwd]
cmd = subprocess.Popen(updateSVN)
status = cmd.wait()

# the same can be achieved in a shorter way:
filelists = [glob.glob(pwd + i + "/*") for i in ('branches', 'tags', 'trunk')]
filelist = sum(filelists, []) # add them together

getAllInfo = ["svn", "info"] + filelist
status = subprocess.call(getAllInfo)

如果您需要捕获子流程的输出,请执行
process = subprocess.Popen(..., stdout=subprocess.PIPE)
data = process.stdout.read()
status = subprocess.wait()

关于python - Python os.popen : How to ensure popen(…) has completed execution before continuing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414575/

相关文章:

multithreading - GCD 中的序列化优先级队列

java - java中线程结束后如何删除文件?

python - 子进程.Popen : cloning stdout and stderr both to terminal and variables

c - 在 C 中打开性能

python:为Popen命令传递多个参数

python - 为什么 While 循环返回一个变量而不返回另一个变量?

python - Windows 10 上的多处理 python 3.6 无法正常工作

python - Flask-admin 在文本字段旁边创建一个按钮

c# - 如何在 .NET ConcurrentDictionary 中实现 remove_if 功能

Python - 从文件中读取第二列