how to kill subprocess when Python process is killed?

Discussion in 'Python' started by mark.v.weber@gmail.com, Aug 7, 2009.

  1. Guest

    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.
     
    , Aug 7, 2009
    #1
    1. Advertising

  2. alex23 Guest

    On Aug 7, 3:42 pm, "" <>
    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
     
    alex23, Aug 7, 2009
    #2
    1. Advertising

  3. >>>>> "" <> (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 <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Aug 7, 2009
    #3
  4. Guest

    On Aug 7, 12:57 am, alex23 <> wrote:
    > On Aug 7, 3:42 pm, "" <>
    > 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!
     
    , Aug 7, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray5531
    Replies:
    1
    Views:
    1,722
    Ray5531
    Jun 21, 2005
  2. Replies:
    0
    Views:
    656
  3. Lars-Erik Aabech
    Replies:
    14
    Views:
    9,132
    hmkjr
    Jul 10, 2009
  4. =?Utf-8?B?UHJhdmVlbiBL?=
    Replies:
    2
    Views:
    4,030
    =?Utf-8?B?UHJhdmVlbiBL?=
    Oct 8, 2004
  5. Ralph Shnelvar
    Replies:
    2
    Views:
    102
    Charles Roper
    Sep 24, 2010
Loading...

Share This Page