By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,264 Members | 1,637 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,264 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Tung Wai Yip <tu********@yahoo.com> wrote in message news:<dm********************************@4ax.com>. ..
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.
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.