Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > popen: how to make sure child process is terminated and get return code?

Reply
Thread Tools

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

 
 
Tung Wai Yip
Guest
Posts: n/a
 
      10-24-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

 
Reply With Quote
 
 
 
 
Anders Dalvander
Guest
Posts: n/a
 
      10-30-2003
Tung Wai Yip <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
pexpect on windows - child process of another child process - quickquestion Z W Python 0 03-09-2013 10:00 PM
How to get a PID of a child process from a process openden with Popen() P.S. Python 0 04-08-2011 10:40 AM
How to make sure child window gets closed SAL ASP .Net 5 10-01-2008 03:42 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments