我想运行一个系统进程,拦截输出,并在Python脚本中逐行实时修改它。
我在打印前等待完成此过程的最佳尝试是:
#!/usr/bin/env python
import subprocess
cmd = "waitsome.py"
proc = subprocess.Popen(cmd, shell=True, bufsize=256, stdout=subprocess.PIPE)
for line in proc.stdout:
print ">>> " + line.rstrip()
脚本waitsome.py
只需每半秒打印一行:
#!/usr/bin/env python
import time
from sys import stdout
print "Starting"
for i in range(0,20):
time.sleep(0.5)
print "Hello, iteration", i
stdout.flush()
是否有一个简单的解决方案让subprocess
允许实时迭代输出?我必须使用线程吗?
曾几何时,我用Perl编写脚本,这是小菜一碟:
open(CMD, "waitsome.py |");
while (<CMD>) {
print ">>> $_";
}
close(CMD);
答案 0 :(得分:14)
循环遍历文件不可避免地会缓冲大量的内容 - 这是所有Python 2. *实现的已知问题。它适用于Python 3.1,最终循环略有不同:
for line in proc.stdout:
print(">>> " + str(line.rstrip()))
如果升级到Python 3.1是不切实际的(我知道它经常是!),那么走另一种方式并以老式的方式编写循环 - 以下版本的循环确实可以像Python中那样工作2 *:
while True:
line = proc.stdout.readline()
if not line:
break
print ">>> " + line.rstrip()
答案 1 :(得分:0)
这整个事情可以封装在迭代器中:
def subprocess_readlines(out):
while True:
line = out.readline()
if not line:
return
yield line
并称为:
for line in proc.stdout:
print ">>>", line.rstrip()