471,312 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to close a program I execute with subprocess.Popen?

I'm writing a program which has to execute a command, get its output
and show it on a treeview.
This command runs for a very long time.
I want to end the execution of the command when the user closes my
application.

Right now I'm using an object my_child of type subprocess.Popen to
execute the command, inside a thread with an infinite loop where we
constantly ask for its output.

To end the program when the user closes the application, I send a
SIGTERM to the process with pid my_child.pid using os.kill. But I also
have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the
pid of /bin/sh -c which is the one which calls the command, because
when I try to run Popen with shell=False, it sends an exception and
says the file or directory doesn't exist.

Anyone knows of a better way to close the command than using a
SIGTERM? I just can't help myself thinking this is an ugly dirty hack.

Jun 29 '07 #1
2 8775
A.T.Hofkamp <ha*@se-162.se.wtb.tue.nlwrote:
In principle, you should only kill your own child processes, your child process
should handle its own childs (your grant child processes). SIGTERM is one way.
Another solution often adopted is to close the stdin of the child.
That is a good idea.

You could also make the parent a process group leader and kill the
process group. You'll get the kill signal too which you'll need to
ignore. That is how the shell keeps track of things IIRC.

Read man setpgid, getpgid should be helful. There are equivalent
commands in os, ie os.setpgid and os.getpgid.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 1 '07 #2
On Jun 29, 6:36 pm, revenan...@hotmail.com wrote:
I'm writing a program which has to execute a command, get its output
and show it on a treeview.
This command runs for a very long time.
I want to end the execution of the command when the user closes my
application.

Right now I'm using an object my_child of type subprocess.Popen to
execute the command, inside a thread with an infinite loop where we
constantly ask for its output.

To end the program when the user closes the application, I send a
SIGTERM to the process with pid my_child.pid using os.kill. But I also
have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the
pid of /bin/sh -c which is the one which calls the command, because
when I try to run Popen with shell=False, it sends an exception and
says the file or directory doesn't exist.

Anyone knows of a better way to close the command than using a
SIGTERM? I just can't help myself thinking this is an ugly dirty hack.
As nick pointed out use process group's .
I use the "preexec_fn" keyword argument to Popen and "os.setsid()"
call's side effect
to make process groups and then os.killpg() to send the signal to
process groups.

child = Popen( cmd , preexec_fn = os.setsid )
os.killpg( child.pid,signal.SIGINT)

Regards
jitu


Jul 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Darren Dale | last post: by
3 posts views Thread by madpython | last post: by
7 posts views Thread by skunkwerk | last post: by
reply views Thread by srinivasan srinivas | last post: by
reply views Thread by srinivasan srinivas | last post: by
25 posts views Thread by Jeremy Banks | last post: by
1 post views Thread by Mark Shewfelt | last post: by
reply views Thread by rosydwin | last post: by

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.