Correctly reading stdout/stderr from subprocess

C

Christoph Haas

Evening,

I'm having trouble with running a process through Python 2.4's
subprocess module. Example code:

========================================================
def run(command):
run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Wait for the process to return
returncode = run.wait()
stdout = run.stdout.readlines()
stderr = run.stderr.readlines()

# Strip newlines at the end of each line
stdout = [line.rstrip('\n') for line in stdout]
stderr = [line.rstrip('\n') for line in stderr]

return returncode,stdout,stderr
========================================================

Unfortunately this def fails when it runs programs that do a lot of
output. I believe this problem has also been described in Bug #1162428.
It says: "You must read away the data before wait() on the process."
Easier said than done. If I put the readlines() call before the
run.wait() then the process hangs, too, waiting for further lines.

Then I read about the communicate() call which handles both
stdout/stderr reading and also waiting for the process to end.
Unfortunately it returned the output char-wise instead of line-wise. I
could certainly re-join the lines. But somehow I feel I'm missing
something simple.

Any ideas?

Kindly
Christoph
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top