how to kill subprocess when Python process is killed?

Piet van Oostrum piet at cs.uu.nl
Fri Aug 7 09:09:29 EDT 2009


>>>>> "mark.v.weber at gmail.com" <mark.v.weber at gmail.com> (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 <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list