Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to kill subprocess when Python process is killed?

Reply
Thread Tools

how to kill subprocess when Python process is killed?

 
 
mark.v.weber@gmail.com
Guest
Posts: n/a
 
      08-07-2009
I am writing a Python program that launches a subprocess (using
Popen).
I am reading stdout of the subprocess, doing some filtering, and
writing to
stdout of the main process.

When I kill the main process (cntl-C) the subprocess keeps running.
How do I kill the subprocess too? The subprocess is likey to run a
long time.

Context:
I'm launching only one subprocess at a time, I'm filtering its stdout.
The user might decide to interrupt to try something else; the user
wants the process and all subprocesses to go away in response
to a cntl-C

I'm new to python; solution must be for Python 2.5 (windows) to help
me.

Any help and/or pointers appreciated.


 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      08-07-2009
On Aug 7, 3:42*pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> When I kill the main process (cntl-C) the subprocess keeps running.
> How do I kill the subprocess too? The subprocess is likey to run a
> long time.


You can register functions to run when the Python process ends by
using the atexit[1] module.

The following has been tested & works under Python 2.6 on Windows XP:

import atexit

def cleanup():
print 'stop the subprocess in here'

atexit.register(cleanup)

while True:
pass


[1]: http://docs.python.org/library/atexit.html
 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      08-07-2009
>>>>> "(E-Mail Removed)" <(E-Mail Removed)> (M) wrote:

>M> I am writing a Python program that launches a subprocess (using
>M> Popen).
>M> I am reading stdout of the subprocess, doing some filtering, and
>M> writing to
>M> stdout of the main process.


>M> When I kill the main process (cntl-C) the subprocess keeps running.
>M> How do I kill the subprocess too? The subprocess is likey to run a
>M> long time.


>M> Context:
>M> I'm launching only one subprocess at a time, I'm filtering its stdout.
>M> The user might decide to interrupt to try something else; the user
>M> wants the process and all subprocesses to go away in response
>M> to a cntl-C


>M> I'm new to python; solution must be for Python 2.5 (windows) to help
>M> me.


>M> Any help and/or pointers appreciated.


When the parent dies, the child should die when it's writing on the
broken pipe. At least that's how it works in Unix systems. I don't know
about Windows, however.

To let the dying be fast you should make sure that stdout in the child
is unbuffered.

--
Piet van Oostrum <(E-Mail Removed)>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
mark.v.weber@gmail.com
Guest
Posts: n/a
 
      08-07-2009
On Aug 7, 12:57*am, alex23 <(E-Mail Removed)> wrote:
> On Aug 7, 3:42*pm, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:
>
> > When I kill the main process (cntl-C) the subprocess keeps running.
> > How do I kill the subprocess too? The subprocess is likely to run a
> > long time.

>
> You can register functions to run when the Python process ends by
> using the atexit[1] module.
>
> The following has been tested & works under Python 2.6 on Windows XP:
>
> * * import atexit
>
> * * def cleanup():
> * * * * print 'stop thesubprocessin here'
>
> * * atexit.register(cleanup)
>
> * * while True:
> * * * * pass
>
> [1]:http://docs.python.org/library/atexit.html


Works perfectly. Thanks!
 
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
subprocess kill luca72 Python 8 12-09-2009 05:24 PM
KeyboardInterrupt should not kill subprocess Michael Goerz Python 1 02-22-2008 01:25 AM
KILL BABY KILL widescreen drsd2kill DVD Video 3 11-29-2004 09:36 PM
Bava's KILL BABY KILL widescreen drsd2kill DVD Video 0 11-27-2004 12:04 AM



Advertisments