read pipe information on a running (popen) process

Jules Stevenson lists at
Tue Jan 6 22:48:59 CET 2009


I'm really struggling with how to read the stdout on a running process. All
the examples I've seen tend to rely on waiting for the process to finish.
The process I'm running takes a while and I need to get stdout [and stderr]
and be able to pipe them into a string for displaying in a web app.

My shoddy code is currently this:

p = subprocess.Popen('xsibatch.exe -s', bufsize=200, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

while p.returncode==None:
    print p.stdout.readline(), p.stderr.readline(),

print 'finished'  

Which results in it printing (i think) the last line written to the pipe
file. I'm thinking there must be a simpler way to access this information.
Ideally I'd like to dump the whole lot to a string at periodic intervals but
am completely stumped as to how to do this as readlines() and many other
methods require an EOF.

On a side note [and much lesser problem currently], I was under the
impression that p.poll() writes the returncode attribute, but from what I
can see when the process terminates p.poll() still returns none?

Many thanks,


More information about the Python-list mailing list