在python中的子进程调用期间使用stdout = subprocess.PIPE时如何获得正常的打印语句执行 [英] how to get the normal print statement execution when using stdout=subprocess.PIPE during subprocess call in python

查看:45
本文介绍了在python中的子进程调用期间使用stdout = subprocess.PIPE时如何获得正常的打印语句执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这个命令在python中启动一个subprocess:

I am starting a subprocess in python through this command:

result = subprocess.Popen([sys.executable, "/subscript.py"] + parameter,stdout=subprocess.PIPE )
result.wait()
out, err = result.communicate()
for line in out.splitlines():
    print line

我用这种方法面临的问题是 subscript.py 中的所有 print 语句都被缓冲到最后并打印在 terminal 在执行结束时.因此,如果我的 subscript.py 执行大约需要 15 分钟,那么在 15 分钟内 terminal 不显示任何内容,一旦 subscript.py 执行结束然后所有的打印语句一起打印.我知道这是因为 stdout=subprocess.PIPE 的使用,如果我删除它,那么我将得到我想要的但我无法删除它,因为我使用它来获取变量的值来自 subscript.py(我在 subscript.py 中打印该变量,然后在父脚本中我将遍历每个打印件以获取带有变量值的打印件).

The problem that I am facing with this approach is that all the print statements in subscript.py are kind of buffered till the end and are printed on the terminal at the end of the execution. So if my subscript.py takes about 15 minutes in execution then for 15 minutes the terminal does not shows anything and once the subscript.py execution ends then all the print statements are printed together. I know this is because of the use of stdout=subprocess.PIPE and if I remove it then I will get what I want but I cannot remove it as I am using it to get the value of a variable from the subscript.py (I am printing that variable in subscript.py and then in parent script I am going through each print to get the print with variable value).

所以,我想要的是所有的打印语句都应该按照它们在 subscript.py 中遇到的方式进行打印,在执行结束之前没有任何缓冲.有没有办法做到这一点?

So, what I want is that all the print statements should be printed as they are encountered in the subscript.py without any buffering till the end of the execution. Is there a way to do that?

更新:我尝试关注 JFSebastian 的方法 但我仍然得到相同的输出.来自子进程的打印被缓冲,一旦子进程结束,它们就会被打印到终端.我拥有的更新代码是:

UPDATE: I tried to follow J.F. Sebastian's method but I still get the same output. The prints from subprocess are buffered and once the subprocess ends then they are printed to the terminal. The updated code I have is:

result = subprocess.Popen([sys.executable, "/subscript.py"] + parameter,stdout=subprocess.PIPE,bufsize=1)

with result.stdout:
        for line in iter(result.stdout.readline, b''):
            print line,
result.wait()

推荐答案

只有在 stdout 缓冲区在子级中刷新后,您链接的答案才会打印任何内容,正如 答案本身.

The answer that you've linked prints anything only after the stdout buffer is flushed in the child as it is explicitly said in the answer itself.

使用 -u 参数,在 Python 子进程中取消缓冲 stdout:

Use -u parameter, to unbuffer stdout in the Python child process:

#!/usr/bin/env python2
import sys
from subprocess import Popen, PIPE

result = Popen([sys.executable, '-u', '/path/to/subscript.py'] + parameters, 
               stdout=PIPE, bufsize=1)
with result.stdout:
    for line in iter(result.stdout.readline, b''):
        print line,
result.wait()

这篇关于在python中的子进程调用期间使用stdout = subprocess.PIPE时如何获得正常的打印语句执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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