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


Groups > comp.lang.python > #52903

Re: Running a command line program and reading the result as it runs

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 <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.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> <kv7g6g$ks8$1@ger.gmane.org> <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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.173.1377275985.19984.python-list@python.org> (permalink)
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

Show key headers only | View raw


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 "<string>", line 1, in <module>
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/

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


Thread

Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-22 15:21 +0930
  Re: Running a command line program and reading the result as it runs Chris Angelico <rosuav@gmail.com> - 2013-08-22 16:22 +1000
    Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-23 00:56 +0930
      Re: Running a command line program and reading the result as it runs Chris Angelico <rosuav@gmail.com> - 2013-08-23 01:33 +1000
        Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-23 16:22 +0930
        Re: Running a command line program and reading the result as it runs Grant Edwards <invalid@invalid.invalid> - 2013-08-23 14:02 +0000
  Re: Running a command line program and reading the result as it runs Rob Wolfe <rw@smsnet.pl> - 2013-08-22 23:14 +0200
    Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-23 16:31 +0930
  Re: Running a command line program and reading the result as it runs Gertjan Klein <gklein@xs4all.nl> - 2013-08-23 11:32 +0200
  Re: Running a command line program and reading the result as it runs Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-08-23 11:53 +0200
  Re: Running a command line program and reading the result as it runs Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-08-23 12:34 +0200
  RE: Running a command line program and reading the result as it runs "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-08-23 10:50 +0000
  Re: Running a command line program and reading the result as it runs Peter Otten <__peter__@web.de> - 2013-08-23 13:14 +0200
    Re: Running a command line program and reading the result as it runs Gertjan Klein <gklein@xs4all.nl> - 2013-08-23 14:03 +0200
    Re: Running a command line program and reading the result as it runs Ian Simcock <Ian.Simcock@Internode.on.net> - 2013-08-24 19:06 +0930
  Re: Running a command line program and reading the result as it runs random832@fastmail.us - 2013-08-23 12:04 -0400
  Re: Running a command line program and reading the result as it runs Peter Otten <__peter__@web.de> - 2013-08-23 18:39 +0200

csiph-web