[]Python管道即时读取输出有关问题
[求助]Python管道即时读取输出问题import subprocess, timepipe subprocess.Popen(myapp.exe file.ext
[求助]Python管道即时读取输出问题
import subprocess, time
pipe = subprocess.Popen('myapp.exe file.ext', stdout=subprocess.PIPE)
while 1:
pipe.stdout.readline()
time.sleep(0.2)
在myapp.exe执行过程中会产生许多Log,我是想能够即时地把Log取出来显示在界面上。
但是发现read(), readlines(), communicate()都是会等待myapp.exe执行完毕,再读取所有输出,达不到“即时”的效果。
现在是用了一个等待循环,每0.2秒用readline()来读取一行显示在界面。
请问有没有更好的方法?更智能点的
[解决办法]pexpect比subprocess更好用一些,你如果能把subprocess换成pexpect.spawn就行
解决你的问题可以使用select和非堵塞io,(pexpect和telnetlib内部大概也都是这样做的)