在 Python 中读取另一个程序的控制台输出 [英] Read console output of another program in Python

查看:63
本文介绍了在 Python 中读取另一个程序的控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个程序 - parent.py 和 myscript.py,如下所示.parent.py 不断在控制台中打印消息.而 myscript.py 需要访问 parent.py 打印的内容.

There are two programs - parent.py and myscript.py, shown below. parent.py keeps printing messages in the console. And myscript.py needs access to what parent.py prints.

parent.py:

import time

while 1:
     time.sleep(1)
     print "test"

myscript.py:

import subprocess
p = None

p = subprocess.Popen(['python', 'parent.py'],
                 stdout=subprocess.PIPE,
                 stderr=subprocess.STDOUT,
                 )
for line in p.stdout:
       print line

我希望 myscript.py 动态捕获 parent.py 的输出.可以看出 parent.py 有一个无限循环 - 脚本永远不会停止,但是每当它输出单词 'test' 时,myscript.py 也应该输出相同的内容.但是 myscript.py 没有给出任何输出.

I want myscript.py to catch the output of parent.py dynamically. As it can be seen that parent.py has an infinite loop - the script is never gonna stop, but whenever it outputs the word 'test', myscript.py should also output the same. But myscript.py doesn't give out any output.

我尝试将 parent.py 中的代码替换为

I tried replacing the code in parent.py with just

print "Hello"

程序执行完毕后,myscript.py 也打印了Hello".所以我猜除非 parent.py 的执行完成,否则 myscript.py 不会读取它.这是怎么回事?

Since the program finished executing, myscript.py also printed "Hello". So I guess unless the execution of parent.py is complete, myscript.py doesn't read it. Is that whats happening?

我还尝试用以下代码段替换 myscript.py 中的循环:

I also tried replacing the loop in myscript.py with this snippet:

for line in iter(p.stdout.readline, ''):
    print line

仍然没有输出.

我的问题是:即使 parent.py 永远不会完成其执行,我如何让 myscript.py 动态读取 parent.py 正在打印的所有内容.

My question is: How do I get myscript.py dynamically read all that is being printed by parent.py even when parent.py is never going to complete its execution.

(我发现了类似的问题,但它们要么不起作用,要么在其中一些问题中根本没有答案.)

(I found similar questions, but they either didn't work or there were no answers at all in some of them.)

推荐答案

这是一个块缓冲问题:当标准输出时重定向到管道 parent.py 将其输出累积在内部缓冲区 (~4K-8K) 中,即,在缓冲区溢出之前,您将在大约一个小时内看不到任何内容.

It is a block buffering issue: when stdout is redirected to a pipe parent.py accumulates its output in an internal buffer (~4K-8K) i.e., you won't see anything for around an hour until the buffer overflows.

要禁用缓冲,请传递 -u 命令行选项:

To disable buffering, pass -u command-line option:

#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE, STDOUT

script_path = os.path.join(get_script_dir(), 'parent.py')
p = Popen([sys.executable, '-u', script_path],
          stdout=PIPE, stderr=STDOUT, bufsize=1)
with p.stdout:
    for line in iter(p.stdout.readline, b''):
        print line,
p.wait()

见:

注意:如果您不需要对输出执行任何操作,则只需删除 stdout=PIPE,即可在控制台中查看输出:

Note: if you don't need to do anything with the output then just drop stdout=PIPE, to see the output in the console:

subprocess.check_call([sys.executable, script_path])

这篇关于在 Python 中读取另一个程序的控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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