how to kill subprocess when Python process is killed?

mark.v.weber at gmail.com mark.v.weber at gmail.com
Fri Aug 7 17:51:48 CEST 2009


On Aug 7, 12:57 am, alex23 <wuwe... at gmail.com> wrote:
> On Aug 7, 3:42 pm, "mark.v.we... at gmail.com" <mark.v.we... at gmail.com>
> 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!



More information about the Python-list mailing list