Python - 从管道中简单的读取行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/965210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:10:13  来源:igfitidea点击:

Python - simple reading lines from a pipe

pythonpipeproducer-consumer

提问by Kiv

I'm trying to read lines from a pipe and process them, but I'm doing something silly and I can't figure out what. The producer is going to keep producing lines indefinitely, like this:

我试图从管道中读取行并处理它们,但我在做一些愚蠢的事情,我无法弄清楚是什么。生产者将无限期地保持生产线,如下所示:

producer.py

生产者.py

import time

while True:
    print 'Data'
    time.sleep(1)

The consumer just needs to check for lines periodically:

消费者只需要定期检查线路:

consumer.py

消费者.py

import sys, time
while True:
    line = sys.stdin.readline()
    if line:
        print 'Got data:', line
    else:
        time.sleep(1)

When I run this in the Windows shell as python producer.py | python consumer.py, it just sleeps forever (never seems to get data?) It seems that maybe the problem is that the producer never terminates, since if I send a finite amount of data then it works fine.

当我在 Windows shell 中运行它时python producer.py | python consumer.py,它只是永远休眠(似乎永远不会获取数据?)似乎问题在于生产者永远不会终止,因为如果我发送有限数量的数据,那么它就可以正常工作。

How can I get the data to be received and show up for the consumer? In the real application, the producer is a C++ program I have no control over.

如何获取要接收的数据并显示给消费者?在实际应用中,生产者是一个我无法控制的C++程序。

采纳答案by Alex Martelli

Some old versions of Windows simulated pipes through files (so they were prone to such problems), but that hasn't been a problem in 10+ years. Try adding a

一些旧版本的 Windows 模拟管道通过文件(因此它们很容易出现此类问题),但这在 10 多年里都不是问题。尝试添加一个

  sys.stdout.flush()

to the producer after the print, and also try to make the producer's stdout unbuffered (by using python -u).

之后到生产者print,并尝试使生产者的标准输出无缓冲(通过使用python -u)。

Of course this doesn't help if you have no control over the producer -- if it buffers too much of its output you're still going to wait a long time.

当然,如果您无法控制生产者,这将无济于事——如果它缓冲了太多的输出,您仍然会等待很长时间。

Unfortunately - while there are many approaches to solve that problem on Unix-like operating systems, such as pyexpect, pexpect, exscript, and paramiko, I doubt any of them works on Windows; if that's indeed the case, I'd try Cygwin, which puts enough of a Linux-like veneer on Windows as to often enable the use of Linux-like approaches on a Windows box.

不幸的是——虽然在类 Unix 操作系统上有很多方法可以解决这个问题,比如 pyexpect、pexpectexscriptparamiko,但我怀疑它们中的任何一个都适用于 Windows;如果确实如此,我会尝试Cygwin,它在 Windows 上放置了足够的类似 Linux 的贴面,以便经常在 Windows 机器上使用类似 Linux 的方法。

回答by Nicolas Dumazet

This is about I/O that is bufferized by default with Python. Pass -uoption to the interpreter to disable this behavior:

这是关于默认使用 Python 缓冲的 I/O。将-u选项传递给解释器以禁用此行为:

python -u producer.py | python consumer.py

It fixes the problem for me.

它为我解决了问题。