Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feed.xsnews.nl!border-1.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.04; 'stops': 0.07; 'subject:How': 0.09; '22,': 0.09; 'blocking': 0.09; 'desired,': 0.09; 'spawn': 0.09; 'cc:addr:python-list': 0.10; 'aug': 0.13; 'child.': 0.16; 'command,': 0.16; 'needed?': 0.16; 'timeout.': 0.16; 'undesirable.': 0.16; 'wake': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'config': 0.17; 'removed.': 0.17; 'working.': 0.17; 'creates': 0.18; 'respective': 0.20; 'trying': 0.21; 'supposed': 0.21; 'sends': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'checking': 0.27; 'message-id:@mail.gmail.com': 0.27; 'run': 0.28; 'efficiently': 0.29; 'parent': 0.29; 'queue': 0.29; 'terminating': 0.29; "they'll": 0.29; "i'm": 0.29; 'becomes': 0.30; 'checked': 0.30; 'implement': 0.32; 'docs': 0.33; 'safely': 0.33; 'problem': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'ability': 0.36; 'created': 0.36; 'but': 0.36; 'child': 0.36; 'should': 0.36; 'problems': 0.36; 'keeps': 0.37; 'uses': 0.37; 'passed': 0.37; 'usual': 0.37; 'rather': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'header:Received:5': 0.40; 'dedicated': 0.61; 'more': 0.63; 'receive': 0.71; 'killing': 0.84; 'dennis': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=fDkzdmr13BwRwQOpbgLbsI6OPYp9vo2abfCagitjCY4=; b=yA7mUBnLCsfowwhJYHJHP9W1dYS/nGLzOcYM02WE5nX40mvEpUnJ0q2OKwIIMMaZt1 Ko8993cUlSJKtS4Di57ZpPXFfUumAwUAQaMYX9TXlukipvAf8okerYv5mMksUQ84w5ms cva7g7OYzJvTjSsJwHUnOzgRPfmNCIw5aiD6pRQ9wIxw9T0YXwOFqwunsaU1v6sFtvsb +0BYLZ7KTpBt5EBxwwMvZBwXBqIucIHQ/hS07L6oDytXtF+TPLTO0q2J6MSH/7ookw/A TW6NEs0SBZ8+jO4Hx5yc3aSO/IV3xy/skRM22qPvs7stTpeBrp+LwPi9jV6IlhXrCANe UnfQ== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 22 Aug 2012 11:46:34 -0600 Subject: Re: How to properly implement worker processes To: Dennis Jacobfeuerborn Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345657626 news.xs4all.nl 6965 [2001:888:2000:d::a6]:38832 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27665 On Wed, Aug 22, 2012 at 11:29 AM, Dennis Jacobfeuerborn wrote: > Hi, > I'm trying to implement a system for periodically checking URLs and I've = run into problems with some of the implementation details. The URLs are sup= posed to be checked continuously until the config for an URL is explicitly = removed. > > The plan is to spawn a worker process for each URL that sends the status = of the last check to its parent which keeps track of the state of all URLs.= When a URL is no longer supposed to be checked the parent process should s= hutdown/kill the respective worker process. > > What I've been going for so far is that the parent process creates a glob= al queue that is passed to all children upon creation which they use to sen= d status messages to the parent. Then for each process a dedicated queue is= created that the parent uses to issue commands to the child. > > The issue is that since the child processes spent some time in sleep() wh= en a command from the parent comes they cannot respond immediately which is= rather undesirable. What I would rather like to do is have the parent simp= ly kill the child instead which is instantaneous and more reliable. > > My problem is that according to the multiprocessing docs if I kill the ch= ild while it uses the queue to send a status to the parent then the queue b= ecomes corrupted and since that queue is shared that means the whole thing = pretty much stops working. > > How can I get around this problem and receive status updates from all chi= ldren efficiently without a shared queue and with the ability to simply kil= l the child process when it's no longer needed? The usual approach to killing worker processes safely is to send them an "exit" command, which they should respond to by terminating cleanly. Instead of using sleep(), have the workers do a blocking get() on the queue with a timeout. This way they'll receive the "exit" message immediately as desired, but they'll still wake up at the desired intervals in order to do their work.