popen: how to make sure child process is terminated and get return code?

Tung Wai Yip tungwaiyip at yahoo.com
Fri Oct 24 15:00:51 EDT 2003


I've build the following utility. It works so far but I want to make
sure I'm doing the right thing. I am running Python 2.3 on windows
2000.

def execute(cmd):
    """
    execute cmd in sub-process, pipe output to stdout,
    return exit status (None means OK)
    """

    cin, cout = os.popen4(cmd)
    cin.close()
    for line in cout:
        sys.stdout.write(line)
    try:
        r = cout.close()
    except IOError:
        # Python 2.3 hack: for reason I don't understand,
        # return code -1 would cause exception in close()
        r = -1
    return r


First it isn't obvious to me how to wait for the child process to
terminate. Does reading all data from cout do the trick?

Secondly, the is some documention on popen() about how to get the
return code. But there is almost none for popen2-4(). What I discover
is it has to close all streams and the last one will give some the
return code.

Third, I find a special case when the return code is -1, it raises a
IOError??? Anyone knows why?



A separate problem is the client problem I called. It is also a python
program that output to both stdout and stderr. The output are in
correct (interleaved) order when running from console. But if I run it
from popen, stdout and stderr seems to be buffered and come out in
incorrect order. I can add some flush() but unless I do it for every
line it still result in incorrect order. Anyone has experience in
this?

import sys

if __name__ == "__main__":
    print >>sys.stdout, "output 1: stdout"
    print >>sys.stderr, "output 2: stderr"
    #sys.stderr.flush()
    r = None
    if len(sys.argv) > 1:
        r = int(sys.argv[1])
    print >>sys.stdout, "output 3: return code is going to be: ", r
    if r != None:
        sys.exit(r)
    print "output 4: no sys.exit() call"


This is one example of output, notice output 2 comes last.
output 1: stdout
output 3: return code is going to be:  None
output 4: no sys.exit() call
output 2: stderr


Thanks a lot for any help!


Wai Yip Tung





More information about the Python-list mailing list