从Python中的另一个进程实时拦截标准输出 [英] Real-time intercepting of stdout from another process in Python

查看:301
本文介绍了从Python中的另一个进程实时拦截标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python脚本中逐行运行系统进程,截取输出并实时对其进行修改.

I'd like to run a system process, intercept the output, and modify it real-time, line by line, in a Python script.

我的最佳尝试是等待该过程在打印之前完成,

My best attempt, which waits for the process to complete before printing, is:

#!/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仅每半秒打印一行:

The script waitsome.py simply prints a line every half a second:

#!/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以允许实时迭代输出?我必须使用线程吗?

Is there an easy solution to get subprocess to allow iterating over the output in real time? Do I have to use threads?

从前,我用Perl编写脚本,这真是小菜一碟:

Once upon a time, I scripted in Perl, and this was a piece of cake:

open(CMD, "waitsome.py |");
while (<CMD>) {
    print ">>> $_";
}
close(CMD);

推荐答案

在文件上循环不可避免地将内容缓冲在很大的块中-这是所有Python 2. *实现的一个已知问题.它可以按照您在Python 3.1中的预期工作,最终循环略有不同:

Looping over a file unavoidably buffers things in pretty large chunks -- a known issue with all Python 2.* implementations. It works as you intend in Python 3.1, with the final loop being slightly different:

for line in proc.stdout:
    print(">>> " + str(line.rstrip()))

如果升级到Python 3.1是不切实际的(并且我知道通常会这样!),请换一种方式,以老式的方式编写循环-以下版本的循环确实可以在Python中正常工作2.*:

If upgrading to Python 3.1 is impractical (and I know it will often be!), go the other way and write the loop in an old-fashioned manner -- the following version of the loop does work as you intend in Python 2.*:

while True:
    line = proc.stdout.readline()
    if not line:
        break
    print ">>> " + line.rstrip()

这篇关于从Python中的另一个进程实时拦截标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆