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

random832 at fastmail.us random832 at fastmail.us
Fri Aug 23 12:04:14 EDT 2013


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.



More information about the Python-list mailing list