好的,我要解决的问题是:
我需要运行一个带有一些标志的程序,检查其进度并向服务器报告。因此,我需要我的脚本在程序执行时避免阻塞,但我也需要能够读取输出。不幸的是,我认为Popen提供的所有方法都无法在没有阻塞的情况下读取输出。我尝试了以下方法,这有点hack-y(我们可以从两个不同的对象读写同一个文件吗?)
import time
import subprocess
from subprocess import *
with open("stdout.txt", "wb") as outf:
with open("stderr.txt", "wb") as errf:
command = ['Path\\To\\Program.exe', 'para', 'met', 'ers']
p = subprocess.Popen(command, stdout=outf, stderr=errf)
isdone = False
while not isdone :
with open("stdout.txt", "rb") as readoutf: #this feels wrong
for line in readoutf:
print(line)
print("waiting...\\r\\n")
if(p.poll() != None) :
done = True
time.sleep(1)
output = p.communicate()[0]
print(output)
不幸的是,Popen似乎只有在命令终止之后才会写入到我的文件中。
有人知道如何解决吗?我不一定非要使用Python,但我确实需要在同一个脚本中向服务器发送POST请求,因此与shell脚本相比,Python似乎更容易一些。
谢谢! Will
Program.exe
是否定期刷新stdout
? - Daniel DiPaolosubprocess.PIPE
吗? - Jochen Ritzel