Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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; 'python3': 0.07; 'see:': 0.07; 'test,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:command': 0.09; 'valueerror:': 0.09; 'python': 0.11; 'itself.': 0.14; 'windows': 0.15; '23,': 0.16; '[new': 0.16; 'fancy': 0.16; 'gained': 0.16; 'iterating': 0.16; 'iteration': 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; 'subject:program': 0.16; 'surprising': 0.16; 'url:file': 0.16; 'url:peps': 0.16; 'version?': 0.16; 'wrote:': 0.18; "python's": 0.19; 'code,': 0.22; 'memory': 0.22; 'aug': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'directory.': 0.24; 'either.': 0.24; 'url:dev': 0.24; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'mix': 0.30; 'code': 0.31; 'lines': 0.31; '"")': 0.31; '"",': 0.31; 'once,': 0.31; 'allows': 0.31; 'file': 0.32; "we're": 0.32; 'linux': 0.33; 'url:python': 0.33; '(most': 0.33; 'fri,': 0.33; 'there,': 0.34; 'maybe': 0.34; 'subject:the': 0.34; 'problem': 0.35; 'but': 0.35; 'there': 0.35; 'returning': 0.36; 'url:org': 0.36; 'wrong': 0.37; 'clear': 0.37; 'performance': 0.37; 'starting': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'confirm': 0.64; 'more': 0.64; 'different': 0.65; 'between': 0.67; 'believe': 0.68; 'benefit': 0.68; 'lose': 0.68; 'led': 0.72; 'otten': 0.84; 'url:cpython': 0.84; '2013,': 0.91; 'system:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Running a command line program and reading the result as it runs Date: Fri, 23 Aug 2013 18:39:47 +0200 Organization: None References: <5215a6cf$0$6512$c3e8da3$5496439d@news.astraweb.com> <1377273854.3835.13339609.6D94109D@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a73e.dip0.t-ipconnect.de User-Agent: KNode/4.7.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377275985 news.xs4all.nl 15997 [2001:888:2000:d::a6]:49854 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52903 random832@fastmail.us wrote: > On Fri, Aug 23, 2013, at 7:14, Peter Otten wrote: >> The following works on my linux system: >> >> instream = iter(p.stdout.readline, "") >> >> for line in instream: >> print line.rstrip() >> >> I don't have Windows available to test, but if it works there, too, the >> problem is the internal buffer used by Python's implementation of file >> iteration rather than the OS. > > I can confirm this on Windows. > > Doesn't this surprising difference between for line in > iter(f.readline,'') vs for line in f violate TOOWTDI? We're led to > believe from the documentation that iterating over a file does _not_ > read lines into memory before returning them. It's not clear to me what > performance benefit can be gained from waiting when there is no more > data available, either. > > I don't understand how it's even happening - from looking at the code, > it looks like next() just calls readline() once, no fancy buffering > specific to itself. Maybe you are looking in the wrong version? For 2.x you can use the file_iternext() function as a starting point, see: http://hg.python.org/cpython/file/1ea833ecaf5a/Objects/fileobject.c#l2316 Python 3 uses a different approach that allows you to mix iteration and readline(): $ python -c 'f = open("tmp.txt"); next(f); f.readline()' Traceback (most recent call last): File "", line 1, in ValueError: Mixing iteration and read methods would lose data $ python3 -c 'f = open("tmp.txt"); next(f); f.readline()' The relevant code is likely in the Modules/_io/ directory. There is also [New I/O] http://www.python.org/dev/peps/pep-3116/