473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
1 7751
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David McNab | last post by:
Hi, When using os.popen, or popen2.Popen3 objects, I see a situation when the child process sometimes doesn't get terminated, even when the child program has exited. It's like Python (2.2.3)...
1
by: Matthew K Jensen | last post by:
I've been trying to find a way to detect when a command run by os.popen (or similar) has completed (in Windows, btw). I found such a function (i don't remember the exact name and library), but the...
2
by: iker.arizmendi | last post by:
On UNIX one can use popen* to get a pipe for reading, a pipe for writing, and the exit code of the child process via a call to close() on the last pipe. Is there any way, in principle, to simulate...
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
1
by: WolfgangZ | last post by:
Hello, I'm starting some subprocesses inside a loop. The processes run independent and dont need any communication between each other. Due to memory issues I need to limit the number of running...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
5
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.