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

T

Tung Wai Yip

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
 
A

Anders Dalvander

Tung Wai Yip said:
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.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top