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

Discussion in 'Python' started by Tung Wai Yip, Oct 24, 2003.

  1. Tung Wai Yip

    Tung Wai Yip Guest

    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
    Tung Wai Yip, Oct 24, 2003
    #1
    1. Advertising

  2. Tung Wai Yip <> wrote in message news:<>...
    > 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.


    Disclaimer: I'm new to Python

    I'm in the same situation as you are, I'm trying to popen4 a program
    and getting the stdout and stderr from it.

    The documentation states that popen[2-4] does not support a way of
    getting the return code. But I've found it to be the return value for
    the last close-call you make on what popen4 returned. I'm also on
    Win2K. I don't know if it is the same for other platforms, but I think
    it should be some sort of standard way of doing this rather common and
    simple thing.
    Anders Dalvander, Oct 30, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    551
  2. SAL
    Replies:
    5
    Views:
    479
    Steven Cheng
    Oct 1, 2008
  3. P.S.
    Replies:
    0
    Views:
    317
  4. Miki Tebeka
    Replies:
    2
    Views:
    595
    Chris Angelico
    Apr 8, 2011
  5. Z W
    Replies:
    0
    Views:
    171
Loading...

Share This Page