Reading from sys.stdin reads the whole file in

Akira Li 4kir4.1i at gmail.com
Thu Aug 28 20:02:38 EDT 2014


Chris Angelico <rosuav at gmail.com> writes:

> On Wed, Aug 27, 2014 at 4:37 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> On Wed, 27 Aug 2014 08:29:20 +0300, Marko Rauhamaa wrote:
>>
>>> Try flushing after each print.
>>
>> Doesn't help.
>
> It does, but insufficiently. If slurp.py is run under Py3, it works
> fine; or take Naoki's suggestion (although without the parens):
>
> import sys
> import time
>
> for line in iter(sys.stdin.readline, ''):
>         print "Time of input:", time.ctime(), line
>         sys.stdin.flush()
>         sys.stdout.flush()
>
> Then it works.
>
> ChrisA

It looks like this bug http://bugs.python.org/issue3907

`python -u out.py | python -u slurp.py`  could be used to avoid .flush()
calls everywhere.

Or reassign `sys.stdin = io.open(sys.stdin.fileno(), 'r', 1)` inside the
script.

http://stackoverflow.com/questions/107705/python-output-buffering


--
Akira




More information about the Python-list mailing list