我学到了在Python中执行命令时,应该使用subprocess模块。 我想要做的是通过ffmpeg对文件进行编码,并监控程序输出,直到文件处理完成。ffmpeg会将进度记录在stderr中。
如果我尝试像这样:
child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
complete = False
while not complete:
stderr = child.communicate()
# Get progress
print "Progress here later"
if child.poll() is not None:
complete = True
time.sleep(2)
在调用child.communicate()后,程序不会继续执行,而是等待命令完成。有没有其他方法可以跟踪输出?
child.poll()
;你可以在结尾处调用child.wait()
。参考链接:https://dev59.com/iXE85IYBdhLWcg3wkkU8#17698359 - jfs