Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'handler': 0.04; 'subject:when': 0.07; 'tends': 0.07; 'complicate': 0.09; 'handlers': 0.09; '(?).': 0.16; '(say': 0.16; 'i/o,': 0.16; 'polls': 0.16; 'ssh,': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'exists.': 0.18; 'tells': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'starts': 0.24; 'code': 0.25; 'performing': 0.26; 'specifically': 0.27; 'cc:addr:gmail.com': 0.28; 'not.': 0.28; '(and': 0.28; 'process,': 0.28; 'tend': 0.28; 'message-id:@mail.gmail.com': 0.28; 'likes': 0.29; 'cc:addr:python.org': 0.29; 'cc:addr:googlegroups.com': 0.30; 'dies': 0.30; 'exiting': 0.30; 'parent': 0.30; 'signals': 0.30; 'threads': 0.30; 'ok,': 0.31; 'idea': 0.32; 'received:209.85.161.46': 0.32; 'received:mail- fx0-f46.google.com': 0.32; "isn't": 0.33; 'cc:2**2': 0.34; 'loop': 0.34; 'setting': 0.34; 'flag': 0.34; 'mix': 0.34; 'something': 0.35; 'sets': 0.35; 'install': 0.35; 'received:209.85.161': 0.36; '...': 0.36; 'checks': 0.37; 'combination': 0.37; 'thread': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'signal': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'itself.': 0.39; 'either': 0.39; 'received:209': 0.40; 'more': 0.61; 'making': 0.67; 'on...': 0.67; 'care': 0.71; 'blocks.': 0.84; 'catches': 0.84; 'dies.': 0.84; 'immune': 0.84; 'kills': 0.84; 'killing': 0.91; 'killed': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=KiVPUdDVvAVqWYX8lbmsW8P4aqDvvq60RPQwYdY5TZE=; b=af9xD9E830jkV/4gN/F+6ADDPTuALueWm5PN9h8Kqnb9GP5ZJ4SkAaUqrTYXQ9E0Bl vqDCCl/F0bbyDNNa0BGiBMhH+apXKcSdsMEq8cfmUZHhSw0QuEL7nY+Nvmo7g83K5Bfq N3FmWPAhPxGa/OmFzZrHNTQUO5QNCHHdBJsYE= MIME-Version: 1.0 In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Date: Wed, 7 Dec 2011 11:50:01 -0800 Subject: Re: Multiprocessing: killing children when parent dies From: Dan Stromberg To: Mihai Badoiu Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org, comp.lang.python@googlegroups.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323287403 news.xs4all.nl 6924 [2001:888:2000:d::a6]:59835 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16790 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.