Re: Multiprocessing: killing children when parent dies

Discussion in 'Python' started by Chris Angelico, Dec 2, 2011.

  1. On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu <> wrote:
    > In the multiprocessing module, on a Process p, by just doing p.daemon=1
    > before p.start(), we can make the child die when the parent exits.  However,
    > the child does not die if the parent gets killed.
    > How can I make sure the child die when the parent gets killed?


    Are you in control of the killing of the parent? One easy way would be
    to catch the signal (say SIGINT) and initiate an orderly shutdown,
    signalling the children first.

    ChrisA
     
    Chris Angelico, Dec 2, 2011
    #1
    1. Advertising

  2. On Friday, December 2, 2011 11:13:34 PM UTC+8, Chris Angelico wrote:
    > On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu <> wrote:
    > > In the multiprocessing module, on a Process p, by just doing p.daemon=1
    > > before p.start(), we can make the child die when the parent exits.  However,
    > > the child does not die if the parent gets killed.
    > > How can I make sure the child die when the parent gets killed?

    >
    > Are you in control of the killing of the parent? One easy way would be
    > to catch the signal (say SIGINT) and initiate an orderly shutdown,
    > signalling the children first.
    >
    > ChrisA


    Multiple thread supporting programming languages in true OOP as Erlang andPython do not talk about POSIX signals.
     
    88888 Dihedral, Dec 2, 2011
    #2
    1. Advertising

  3. On Friday, December 2, 2011 11:13:34 PM UTC+8, Chris Angelico wrote:
    > On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu <> wrote:
    > > In the multiprocessing module, on a Process p, by just doing p.daemon=1
    > > before p.start(), we can make the child die when the parent exits.  However,
    > > the child does not die if the parent gets killed.
    > > How can I make sure the child die when the parent gets killed?

    >
    > Are you in control of the killing of the parent? One easy way would be
    > to catch the signal (say SIGINT) and initiate an orderly shutdown,
    > signalling the children first.
    >
    > ChrisA


    Multiple thread supporting programming languages in true OOP as Erlang andPython do not talk about POSIX signals.
     
    88888 Dihedral, Dec 2, 2011
    #3
  4. On Sat, Dec 3, 2011 at 2:57 AM, 88888 Dihedral
    <> wrote:
    > Multiple thread supporting programming languages  in true OOP as Erlangand Python do not talk about POSIX signals.


    The OP talked about multiprocessing. Each thread of execution is a
    separate context, and can receive signals.

    ChrisA
     
    Chris Angelico, Dec 2, 2011
    #4
  5. Please check Erlang that spawn so easily. And there are Python packages can do the same task.
     
    88888 Dihedral, Dec 2, 2011
    #5
  6. Please check Erlang that spawn so easily. And there are Python packages can do the same task.
     
    88888 Dihedral, Dec 2, 2011
    #6
  7. On 12/7/11, Mihai Badoiu <> wrote:
    > ok, so the code is something like
    > #process A
    > p = Process(...)
    > p.daemon = 1
    > p.start() # starts process B
    > ...
    >
    > If process A dies (say error, or ctrl-c), or finishes, then process B also
    > dies. But if process A is killed with the "kill" command, then process B
    > soldiers on...
    >
    > Any idea on how to make process B die when process A gets killed by the
    > "kill" command?


    1) If all you care about is SIGTERM, SIGHUP and the like (and
    specifically NOT SIGKILL), you could just install a signal handler
    that catches any catchable signals you're interested in. Then the
    signal either kills the children directly, or sets a flag that tells
    the main process to do some killing shortly. Note that threads and
    signal handlers don't mix very well - the combination tends to make
    the main thread immune to control-C, whether you want it to be or not.
    Also, signal handlers tend to complicate performing I/O, as you're
    more likely to read short blocks.

    2) If you need to handle SIGKILL gracefully, and you have access to
    the code of the child process, you could make sure that the child
    isn't setting a SID (?). ssh, I believe, likes to start a new SID,
    making it immune to signals to the parent. Alternatively, you could
    add something to the child process' main loop that polls the parent,
    exiting if the parent no longer exists.

    3) If you need to handle SIGKILL gracefully, and you don't have access
    to the code of the child process, you could use a single extra process
    that checks for the presense of the parent, and if it doesn't exist
    any more, then kill the children before exiting itself.
     
    Dan Stromberg, Dec 7, 2011
    #7
    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. Joel Lindsey
    Replies:
    1
    Views:
    1,526
    Steve W. Jackson
    Sep 22, 2003
  2. Christopher M. Lusardi
    Replies:
    3
    Views:
    217
    Alan J. Flavell
    Oct 19, 2003
  3. Paul Clements
    Replies:
    2
    Views:
    214
    Ben Morrow
    Feb 11, 2004
  4. Марк Коренберг

    Multiprocessing: killing children when parent dies

    Марк Коренберг, Oct 18, 2013, in forum: Python
    Replies:
    2
    Views:
    108
    Ricardo Aráoz
    Oct 18, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    111
    Terry Reedy
    Oct 18, 2013
Loading...

Share This Page