Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mentioned,': 0.07; 'python3': 0.07; 'subject:file': 0.07; 'sys': 0.07; 'latter': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"python': 0.16; '(note': 0.16; '__future__': 0.16; 'buffering,': 0.16; 'flush': 0.16; 'former,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'line)': 0.16; 'other,': 0.16; 'seconds,': 0.16; 'stdout': 0.16; 'subject:Reading': 0.16; 'world!': 0.16; 'demonstrate': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'properly': 0.19; '(the': 0.22; 'import': 0.22; 'aug': 0.22; 'issue.': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'correctly.': 0.31; "d'aprano": 0.31; 'pipe': 0.31; 'steven': 0.31; 'know.': 0.32; 'subject:the': 0.34; 'subject:from': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'two': 0.37; 'easily': 0.37; 'problems': 0.38; 'files': 0.38; 'pm,': 0.38; 'expect': 0.39; 'though,': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'even': 0.60; "you're": 0.61; 'happen': 0.63; 'different': 0.65; 'cut': 0.74; 'arrive,': 0.84; 'consumer,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=HdpB74OGKR+BZs/QbNxZrvhDuxMrX5+1Nn9ac3u5Md8=; b=rbG02pZLAUtDccjwbaKYdNgd5o2+u0BTSkjwMukiu/lwqabUCdHSgT8oOFYqHHc8rS pjnaRY/w2HxJXdskxlX3no44apYvEHb/xLZQnfigF3VM60q6k/1EqbU9d5yIoIosXKUj +fc6y4oxJ0S5ove4GJMmsdzjF5v2g3ubFy1pf+Q6EWWd2HzTZ2BKYtgykuBdo0uV/QZ2 toUuwtKZg8Wc4idaExGDJ9qZHSewVvOvSfYbOqW5Jkt6NZf1ujY0ENlcPmWSNa9/xRFc PWno69kgiaB7hNDchF97WvyAvQBrr3/zHXf1hWVziDDZDYzA6TLx0ny7Zvs1IfHqHo3a 9mdA== MIME-Version: 1.0 X-Received: by 10.43.79.135 with SMTP id zq7mr9409391icb.33.1409119377503; Tue, 26 Aug 2014 23:02:57 -0700 (PDT) In-Reply-To: <53fd6a48$0$11111$c3e8da3@news.astraweb.com> References: <53fd6a48$0$11111$c3e8da3@news.astraweb.com> Date: Wed, 27 Aug 2014 16:02:57 +1000 Subject: Re: Reading from sys.stdin reads the whole file in From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409119385 news.xs4all.nl 2926 [2001:888:2000:d::a6]:34913 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77104 On Wed, Aug 27, 2014 at 3:19 PM, Steven D'Aprano wrote: > When I pipe one to the other, I expect each line to be printed as they > arrive, but instead they all queue up and happen at once: You're seeing two different problems here. One is the flushing of stdout in out.py, as Marko mentioned, but it's easily proven that that's not the whole issue. Compare "python out.py" and "python out.py|cat" - the latter will demonstrate whether or not it's getting flushed properly (the former, where stdout is a tty, will always flush correctly). But even with that sorted, iterating over stdin has issues in Python 2. Here's a tweaked version of your files (note that I cut the sleeps to 2 seconds, but the effect is the same): rosuav@sikorsky:~$ cat out.py import time print("Hello...",flush=True) time.sleep(2) print("World!",flush=True) time.sleep(2) print("Goodbye!",flush=True) rosuav@sikorsky:~$ cat slurp.py from __future__ import print_function import sys import time for line in sys.stdin: print(time.ctime(), line) rosuav@sikorsky:~$ python3 out.py|python slurp.py Wed Aug 27 16:00:16 2014 Hello... Wed Aug 27 16:00:16 2014 World! Wed Aug 27 16:00:16 2014 Goodbye! rosuav@sikorsky:~$ python3 out.py|python3 slurp.py Wed Aug 27 16:00:19 2014 Hello... Wed Aug 27 16:00:21 2014 World! Wed Aug 27 16:00:23 2014 Goodbye! rosuav@sikorsky:~$ With a Py2 consumer, there's still buffering happening. With a Py3 consumer, it works correctly. How to control the Py2 buffering, though, I don't know. ChrisA