[英]Python - simple reading lines from a pipe
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 producer.py
import time
while True:
print 'Data'
time.sleep(1)
The consumer just needs to check for lines periodically: 消费者只需要定期检查线路:
consumer.py consumer.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
当我在Windows shell中以python producer.py | python consumer.py
运行它时 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. 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 ++程序。
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. 一些旧版本的Windows通过文件模拟管道(因此它们容易出现这样的问题),但这已经不是10年多的问题了。 Try adding a 尝试添加一个
sys.stdout.flush()
to the producer after the print
, and also try to make the producer's stdout unbuffered (by using python -u
). print
后到生产者,并尝试使生成器的stdout无缓冲(通过使用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; 不幸的是 - 虽然有许多方法可以在类Unix操作系统上解决这个问题,比如pyexpect, pexpect , exscript和paramiko ,但我怀疑它们中的任何一个都适用于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. 如果确实如此,我会尝试Cygwin ,它在Windows上放置了类似Linux的单板,以便经常在Windows机器上使用类似Linux的方法。
This is about I/O that is bufferized by default with Python. 这是关于默认使用Python缓存的I / O. Pass -u
option to the interpreter to disable this behavior: 将-u
选项传递给解释器以禁用此行为:
python -u producer.py | python consumer.py
It fixes the problem for me. 它解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.