Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16790

Re: Multiprocessing: killing children when parent dies

References (2 earlier) <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <mailman.3228.1322842458.27778.python-list@python.org> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> <CAAdqOWHY0S0uaX-OuXR8KQCkAtUPvYZZMoDBwhxO0PhTsVoF8g@mail.gmail.com> <CAGGWHE7=LTQ==HzqroetGUvk=kdCYJCnNm=pKsNUtVd+9a9kKA@mail.gmail.com>
Date 2011-12-07 11:50 -0800
Subject Re: Multiprocessing: killing children when parent dies
From Dan Stromberg <drsalists@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3384.1323287403.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 12/7/11, Mihai Badoiu <mbadoiu@gmail.com> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Multiprocessing: killing children when parent dies Chris Angelico <rosuav@gmail.com> - 2011-12-03 02:13 +1100
  Re: Multiprocessing: killing children when parent dies 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-02 07:57 -0800
  Re: Multiprocessing: killing children when parent dies 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-02 07:57 -0800
    Re: Multiprocessing: killing children when parent dies Chris Angelico <rosuav@gmail.com> - 2011-12-03 03:14 +1100
      Re: Multiprocessing: killing children when parent dies 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-02 08:27 -0800
      Re: Multiprocessing: killing children when parent dies 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-02 08:27 -0800
        Re: Multiprocessing: killing children when parent dies Dan Stromberg <drsalists@gmail.com> - 2011-12-07 11:50 -0800

csiph-web