Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to close a program I execute with subprocess.Popen? (http://www.velocityreviews.com/forums/t518562-how-to-close-a-program-i-execute-with-subprocess-popen.html)

revenant81@hotmail.com 06-29-2007 01:36 PM

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.


A.T.Hofkamp 06-29-2007 03:14 PM

Re: How to close a program I execute with subprocess.Popen?
 
On 2007-06-29, revenant81@hotmail.com <revenant81@hotmail.com> wrote:
> 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


Maybe that holds for a system recently started and mostly idle, but it need
not be the case. If you have more users active, and each user is forking
processes, (or one user is forking several processes concurrently), the order
of assigning process IDs is not defined.
For systems that run for a longer time, unused process IDs are re-used, skipping
over process IDs that are still living.

> 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.


The shell performs a search over PATH to find the command executable (ie it
maps 'ls' to '/bin/ls'). If you provide a full path to the command you are
starting, you don't need the shell.

> 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.


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. This
notifies the child that no more data will arrive, and many commands react on
that message by terminating. Try running

command < /dev/null

/dev/null is the empty input stream.

Last but not least, many commands do something special when sent -HUP. Look in
the manual page of the command for clean ways to close the command down.


Albert

Nick Craig-Wood 07-01-2007 08:30 AM

Re: How to close a program I execute with subprocess.Popen?
 
A.T.Hofkamp <hat@se-162.se.wtb.tue.nl> wrote:
> 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 <nick@craig-wood.com> -- http://www.craig-wood.com/nick

jitudon@hotmail.com 07-06-2007 05:14 AM

Re: How to close a program I execute with subprocess.Popen?
 
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






All times are GMT. The time now is 10:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.