How do i get output from pope3 while process is running?

Andrew Patterson andrew at fc.hp.com
Fri Dec 3 18:57:30 EST 1999


I want to run an extremely long running external command and send its output

to stdout of my python program.  I started with os.system which works fine

except that I needed to capture the stderr output in a separate stream.  The

popen2.Popen3 class seems to fit the bill, however, I can not get any of the

output until the process finishes (hours later).  Here is the code I am using:

################################  run_proc()  #################################
def run_proc(cmd, capturestdout=0, capturestderr=0, printstdout=0):
    """Run an external command and capture stderr, stdout, and exit code."""

    proc = popen2.Popen3(cmd, capturestderr)
    stdoutlist = []
    stderrlist = []
    if capturestdout:
        for line in proc.fromchild.readlines():
            if printstdout:
                print "\n********* Got Here ************"
                sys.stdout.write(line)
            stdoutlist.append(line)
    if capturestderr:
        stderrlist = proc.childerr.readlines()
    exit_code = proc.wait()
    return (exit_code, stdoutlist, stderrlist)

I get the "Got here" lines after the process finishes.  I am running python 1.5.2

on an HP-UX 11.0 system with native pthreads compiled in.

Can someone please tell me what I am doing wrong.  Thanks

Andrew
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
   Andrew Patterson                          Voice:  (970) 898-3261
   Hewlett-Packard Company/DSL               FAX:    (970) 898-2180
   3404 East Harmony Road / MS 7             email: andrew at fc.hp.com
   Fort Collins, Colorado  80525







More information about the Python-list mailing list