read pipe information on a running (popen) process

  • Thread starter Jules Stevenson
  • Start date
J

Jules Stevenson

Hi,

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:
p.poll()
print p.stdout.readline(), p.stderr.readline(),
time.sleep(1)

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,

Jules
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top