popen question
Karthik Gurusamy
kar1107 at gmail.com
Tue Jan 8 19:58:54 EST 2008
On Jan 8, 1:20 am, Robert Latest <boblat... at yahoo.com> wrote:
> Hello,
>
> look at this function:
>
> --------------
> def test():
> child = os.popen('./slow')
> for line in child:
> print line
> -------------
>
> The program "slow" just writes the numbers 0 through 9 on stdout, one line a
> second, and then quits.
>
> I would have expected the python program to spit out a numbers one by one,
> instead I see nothing for 10 seconds and then the whole output all at once.
>
> How can I get and process the pipe's output at the pace it is generated?
I've seen this problem and it took me a while to figure out what is
happening.
As other posts, I too first suspected it's a problem related to line/
full buffering on the sender side (./slow here).
It turned out that the "for line in child:" line in the iterator is
the culprit. The iterator does something like a child.readlines()
underneath (in it's __iter__ call) instead of a more logical single
line read.
Change your reading to force line-by-line read
e.g.
While True:
line = child.readline()
if not line: break
print line
Karthik
>
> Thanks,
>
> robert
More information about the Python-list
mailing list