Multiprocessing: killing children when parent dies

Mihai Badoiu mbadoiu at gmail.com
Wed Dec 7 14:53:47 EST 2011


I like 2) the most.  I do have access to the child.  The child is a process
started with multiprocessing.Process(function).  How do I _not_ set an SID?

thanks,

--mihai

On Wed, Dec 7, 2011 at 2:50 PM, Dan Stromberg <drsalists at gmail.com> wrote:

> On 12/7/11, Mihai Badoiu <mbadoiu at 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111207/477deb12/attachment.html>


More information about the Python-list mailing list