Trouble with popen2

Rembrandt Q Einstein hercules.rockefeller at springfield.??.us
Mon Sep 27 21:13:00 CEST 2004

I am running an external command and I need to know a) when it is done 
and b) what it wrote to both stdout and stderr.  After a little 
searching, I found the popen2 module and used the Popen3 class.  I'm 
having trouble with it hanging, though.

Here is a very well put (by someone else) posting that describes some 

I came to a similar conclusion as that poster and his workaround 
(independently discovered by me) does do the job he requires.  However, 
I need to also read stderr, so I made this sample writer and runner:


import sys

for line in range(0, int(sys.argv[1])):
     print "X" * 120

print >>sys.stderr, "hi"

import popen2

f = popen2.Popen3("./ 50000", True)
outs = []
errs = []
while (f.poll() == -1):
     errs += f.childerr.readlines()
     outs += f.fromchild.readlines()

This hangs in the childerr.readlines().  Is it blocking?  If so, why? 
In any case, how can I be sure to read ALL data from BOTH stderr and 
stdout and not be in danger of hanging?

PS: I just found that if I swap the order of the readlines() statements, 
it works.  But I don't want to use that until I understand why.  I 
suspect it's a race condition and I don't want to rely on that.

More information about the Python-list mailing list