Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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; '\xe2\x80\x94': 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; 'iterating': 0.16; 'line)': 0.16; 'other,': 0.16; 'seconds,': 0.16; 'slow,': 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; 'email addr:gmail.com>': 0.22; 'issue.': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'url:mailman': 0.30; 'getting': 0.31; 'lines': 0.31; 'correctly.': 0.31; "d'aprano": 0.31; 'pipe': 0.31; 'steven': 0.31; 'know.': 0.32; 'url:python': 0.33; 'subject:the': 0.34; 'subject:from': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'url:listinfo': 0.36; 'url:org': 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; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; "you're": 0.61; 'header:Message-Id:1': 0.63; 'happen': 0.63; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'mailbox': 0.68; 'cut': 0.74; 'arrive,': 0.84; 'consumer,': 0.84; 'tricky': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:mime-version:message-id:in-reply-to:references:from:to:cc :subject:content-type; bh=Q66VXGvL+OfvL7bBHsgWJC+bna2dc7/ha2FD1WtEe/Y=; b=I369txDTNpbJvUX2tSmMx8r+IUxf2JQk2fsXS4xGxTlWuJZPe8+dq2hABQSYXUL8Kh fYxhDofiu48vSS1XUY6m9ZlqnOyE/qmoTmF6tTXSq+GIXxpgzxdUIXo1db9r6TgNV1LO xhoijNepWDddJkEaAopYMgg80Ndc6QfcJgpdLCmMV+xMn0cUc9fEi+r43+CJ75M2ihaa XumMypVEmW0WKZQmV0we0cRZEzQrS9hhHbUsCXBav4LS0qdaLNSQAt37EGQGq9XgJKze 2bovy0wpWZUkaFdSHs0VtnQk3TmLKiQMiS+mMKZNQfnqzEIkEN1KIoPISTdhCwbHxho2 E0hA== X-Received: by 10.140.37.181 with SMTP id r50mr14727228qgr.100.1409119656392; Tue, 26 Aug 2014 23:07:36 -0700 (PDT) Date: Tue, 26 Aug 2014 23:07:36 -0700 (PDT) X-Google-Original-Date: Wed, 27 Aug 2014 06:07:35 GMT MIME-Version: 1.0 X-Mailer: Nodemailer (0.5.0; +http://www.nodemailer.com/) In-Reply-To: References: X-Orchestra-Oid: 98480699-6C37-4859-A2B8-680932D067DB X-Orchestra-Sig: 7509d3076e836bd1c8fa3b9bde042fcc588bcf6d X-Orchestra-Thrid: T9A8D81D7-4520-4662-8755-94CDB7973859_1477566102499318705 X-Orchestra-Thrid-Sig: eda0a46f5c90a277fffd100012fa8893293ebd8b X-Orchestra-Account: 9e69dac186421f04d6595e6a27785f61abfdbe7c From: "Naoki INADA" To: rosuav@gmail.com Subject: Re: Reading from sys.stdin reads the whole file in Content-Type: multipart/alternative; boundary="----Nodemailer-0.5.0-?=_1-1409119656022" Cc: python-list@python.org 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: 125 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409119664 news.xs4all.nl 2956 [2001:888:2000:d::a6]:37734 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77105 ------Nodemailer-0.5.0-?=_1-1409119656022 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable I recommend Python 3. On Python 2, iterating lines without buffering is slow, tricky and ugly. for line in iter(sys.stdin.readline(), ''): =C2=A0 =C2=A0 print line =E2=80=94 Sent from Mailbox On Wed, Aug 27, 2014 at 3:03 PM, Chris Angelico wrote: > 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 =22python out.py=22 and =22python > out.py|cat=22 - 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(=22Hello...=22,flush=3DTrue) > time.sleep(2) > print(=22World!=22,flush=3DTrue) > time.sleep(2) > print(=22Goodbye!=22,flush=3DTrue) > rosuav@sikorsky:~$ cat slurp.py > from =5F=5Ffuture=5F=5F import print=5Ffunction > 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 > --=20 > https://mail.python.org/mailman/listinfo/python-list ------Nodemailer-0.5.0-?=_1-1409119656022 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
I recommend Python 3.
On Python 2, iterating lines without buffering is slow, tricky and = ugly.

for line in iter(sys.stdin.readline(), ''):
=C2=A0 =C2=A0 print line
=E2=80=94
Sent from Mailbox


On Wed, Aug 27, 2014 at 3:03 PM= , Chris Angelico <rosuav@gmail.com> = wrote:

On Wed, Aug 27, = 2014 at 3:19 PM, Steven D'Aprano <steve@pearwood.info> 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 =22python out.py=22 and =22python
out.py|cat=22 - 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(=22Hello...=22,flush=3DTrue)
time.sleep(2)
print(=22World!=22,flush=3DTrue)
time.sleep(2)
print(=22Goodbye!=22,flush=3DTrue)
rosuav@sikorsky:~$ cat slurp.py
from =5F=5Ffuture=5F=5F import print=5Ffunction
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
--=20
https://mail.python.org/mailman/listinfo/python-list


------Nodemailer-0.5.0-?=_1-1409119656022--