Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77115

Re: Reading from sys.stdin reads the whole file in

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; '"""': 0.07; 'binary': 0.07; 'subject:file': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'iterators': 0.16; 'loop.': 0.16; 'matters,': 0.16; 'outputs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'simulate': 0.16; 'stderr': 0.16; 'stdout': 0.16; 'subject:Reading': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'environment': 0.24; 'switch': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; "d'aprano": 0.31; 'option.': 0.31; 'steven': 0.31; 'totally': 0.33; 'subject:the': 0.34; 'subject:from': 0.34; 'there': 0.35; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'slowly': 0.60; 'here': 0.66; 'said:': 0.68
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Reading from sys.stdin reads the whole file in
Date Wed, 27 Aug 2014 09:42:40 +0200
Organization None
References <53fd6a48$0$11111$c3e8da3@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p57bd8be8.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13497.1409125378.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409125378 news.xs4all.nl 2972 [2001:888:2000:d::a6]:48622
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77115

Show key headers only | View raw


Steven D'Aprano wrote:

> I'm trying to read from stdin. Here I simulate a process that slowly
> outputs data to stdout:
> 
> steve@runes:~$ cat out.py
> import time
> 
> print "Hello..."
> time.sleep(10)
> print "World!"
> time.sleep(10)
> print "Goodbye!"

In addition to what already has been said: you can switch off output 
buffering of stdout/stderr with

python -u out.py

or by setting the PYTHONUNBUFFERED environment variable.

You still need the readline trick to get unbuffered input. Quoting the man-
page:

"""

       -u     Force stdin, stdout and stderr to be totally  unbuffered.   On
              systems where it matters, also put stdin, stdout and stderr in
              binary mode.  Note that there is internal buffering in  xread‐
              lines(),  readlines()  and file-object iterators ("for line in
              sys.stdin") which is not influenced by this option.   To  work
              around  this,  you  will  want  to  use "sys.stdin.readline()"
              inside a "while 1:" loop.
"""

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Reading from sys.stdin reads the whole file in Steven D'Aprano <steve@pearwood.info> - 2014-08-27 05:19 +0000
  Re: Reading from sys.stdin reads the whole file in Marko Rauhamaa <marko@pacujo.net> - 2014-08-27 08:29 +0300
    Re: Reading from sys.stdin reads the whole file in Marko Rauhamaa <marko@pacujo.net> - 2014-08-27 08:31 +0300
    Re: Reading from sys.stdin reads the whole file in Steven D'Aprano <steve@pearwood.info> - 2014-08-27 06:37 +0000
      Re: Reading from sys.stdin reads the whole file in Chris Angelico <rosuav@gmail.com> - 2014-08-27 16:45 +1000
      Re: Reading from sys.stdin reads the whole file in Akira Li <4kir4.1i@gmail.com> - 2014-08-29 04:02 +0400
  Re: Reading from sys.stdin reads the whole file in Chris Angelico <rosuav@gmail.com> - 2014-08-27 16:02 +1000
  Re: Reading from sys.stdin reads the whole file in Peter Otten <__peter__@web.de> - 2014-08-27 09:42 +0200
    Re: Reading from sys.stdin reads the whole file in Marko Rauhamaa <marko@pacujo.net> - 2014-08-27 11:39 +0300
      Re: Reading from sys.stdin reads the whole file in Peter Otten <__peter__@web.de> - 2014-08-27 11:31 +0200

csiph-web