Python 中实时拦截来自另一个进程的stdout [英] Real-time intercepting of stdout from another process in Python

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

问题描述

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

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

我最好的尝试是:

#!/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 中实时拦截来自另一个进程的stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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