简体   繁体   English

从子进程获取未缓冲的输出不起作用Python

[英]Getting the output unbuffered from subprocess is not working Python

I have an exectuable that if i run it with flags it starts a tool that generate messages all the time. 我有一个可执行程序,如果我用标志运行它,它将启动一个始终生成消息的工具。 When i try to run it with subprocess i get the output to the cmd and that is fine. 当我尝试使用子进程运行它时,我将输出输出到cmd,这很好。 The thing is that i want to take this messages and do something with them but for some reason the unbuffring method is not working and I'm not getting any message . 问题是我想接收这些消息并对它们执行某些操作,但是由于某种原因,取消缓冲的方法不起作用,并且我也没有收到任何消息。

The following is the code that I tried : 以下是我尝试过的代码:

p = subprocess.Popen(
 ["my_tool.exe","runMode","y","port","1234"],stdout=subprocess.PIPE,stderr=subprocess.STDOUT, bufsize=1)

To generate the output tried the folloing: 要生成输出,请尝试以下操作:

for line in p.stdout:
    print("CUR", line)

and: 和:

while p.poll() is None:
    line = p.stdout.read(2)
    print("Print:" + line)

and: 和:

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

Any idea why the code blocks on the stdout line alwayes ? 知道为什么代码总是在stdout行上阻塞吗?

UPDATE: 更新:

For example if i have the following infinite program: 例如,如果我有以下无限程序:

inf.py INF

import time
i = 0
while True:
    time.sleep(0.5)
    print(i)
    i += 1

and the following code that runs it: 以及运行它的以下代码:

p = subprocess.Popen(r"C:\Python35\python.exe inf.py",
                     stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)

and to generate the output I'm using one of the above methods , If i stop it using CTRL+ C it is alwayes stuck on this line: 并使用上述方法之一生成输出,如果我使用CTRL + C停止它,则始终停留在此行上:

  File "C:\Python35\lib\encodings\cp1252.py", line 22, in decode
    def decode(self, input, final=False):

First I set the following environment variable: PYTHONUNBUFFERED=1 首先,我设置以下环境变量: PYTHONUNBUFFERED=1

Then I changed your code a little: 然后,我对您的代码做了一些更改:

read_infinite.py read_infinite.py

import subprocess

binary = "C:\\Python35\\python.exe"
script = "C:\\temp\\python\\tests\\inf.py"

p = subprocess.Popen([binary, script], stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)

while p.poll() is None:
    #line = p.stdout.read(2)
    line = p.stdout.readline()
    if line:
        print("Result: %r" % line)

Removing bufsize=1 works same. 删除bufsize=1工作原理相同。

The error I got at first was that the inf.py was not found, but this was not shown, because the prints of read_infinite.py scrolled the error out. 我最初遇到的错误是没有找到inf.py,但是没有显示出来,因为read_infinite.py的打印将错误滚动了出去。 And the process stopped after a lot of lines of "Result:" (variable "line" is empty). 并且在“结果:”的许多行之后,过程停止了(变量“行”为空)。

Removing if line: and adding time.sleep(0.5) after the print (remember import time !) shows the same results. if line:删除if line:并在打印后添加time.sleep(0.5) (请记住import time !),则会显示相同的结果。

So the solution is: the parent has to wait for the child. 因此解决方案是: 父母必须等待孩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM