Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!nntp.giganews.com!feed.xsnews.nl!border-3.ams.xsnews.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.04; 'exit': 0.07; 'stops': 0.07; 'subject:How': 0.09; '22,': 0.09; 'blocking': 0.09; 'desired,': 0.09; 'fetch': 0.09; 'spawn': 0.09; 'to:addr:comp.lang.python': 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; 'wednesday,': 0.22; 'work.': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'cc:addr:gmail.com': 0.27; 'checking': 0.27; "doesn't": 0.28; 'actual': 0.28; '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; 'could': 0.32; 'docs': 0.33; 'rid': 0.33; 'safely': 0.33; 'problem': 0.33; 'hi,': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'ability': 0.36; 'created': 0.36; 'but': 0.36; 'child': 0.36; 'should': 0.36; 'problems': 0.36; 'keeps': 0.37; 'option': 0.37; 'uses': 0.37; 'passed': 0.37; 'usual': 0.37; 'rather': 0.37; 'received:209': 0.37; 'far': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'dedicated': 0.61; 'safe': 0.63; 'more': 0.63; 'august': 0.66; 'receive': 0.71; 'killing': 0.84; 'received:209.85.216.184': 0.84; 'dennis': 0.91 Newsgroups: comp.lang.python Date: Wed, 22 Aug 2012 12:40:25 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=93.207.121.228; posting-account=_b6A3QoAAABPwmSQu8QIyLK7idqo6rsL References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 93.207.121.228 MIME-Version: 1.0 Subject: Re: How to properly implement worker processes From: Dennis Jacobfeuerborn To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, Dennis Jacobfeuerborn 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: , Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345664434 news.xs4all.nl 6898 [2001:888:2000:d::a6]:33494 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27679 On Wednesday, August 22, 2012 7:46:34 PM UTC+2, Ian wrote: > On Wed, Aug 22, 2012 at 11:29 AM, Dennis Jacobfeuerborn >=20 > wrote: >=20 > > Hi, >=20 > > I'm trying to implement a system for periodically checking URLs and I'v= e run into problems with some of the implementation details. The URLs are s= upposed to be checked continuously until the config for an URL is explicitl= y removed. >=20 > > >=20 > > The plan is to spawn a worker process for each URL that sends the statu= s of the last check to its parent which keeps track of the state of all URL= s. When a URL is no longer supposed to be checked the parent process should= shutdown/kill the respective worker process. >=20 > > >=20 > > What I've been going for so far is that the parent process creates a gl= obal queue that is passed to all children upon creation which they use to s= end status messages to the parent. Then for each process a dedicated queue = is created that the parent uses to issue commands to the child. >=20 > > >=20 > > The issue is that since the child processes spent some time in sleep() = when 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 si= mply kill the child instead which is instantaneous and more reliable. >=20 > > >=20 > > My problem is that according to the multiprocessing docs if I kill the = child while it uses the queue to send a status to the parent then the queue= becomes corrupted and since that queue is shared that means the whole thin= g pretty much stops working. >=20 > > >=20 > > How can I get around this problem and receive status updates from all c= hildren efficiently without a shared queue and with the ability to simply k= ill the child process when it's no longer needed? >=20 >=20 >=20 > The usual approach to killing worker processes safely is to send them >=20 > an "exit" command, which they should respond to by terminating >=20 > cleanly. Instead of using sleep(), have the workers do a blocking >=20 > get() on the queue with a timeout. This way they'll receive the >=20 > "exit" message immediately as desired, but they'll still wake up at >=20 > the desired intervals in order to do their work. I was thinking about something like that but the issue is that this really = only works when you don't do any actual blocking work. I may be able to get= around the sleep() but then I have to fetch the URL or do some other work = that might block for a while so the get() trick doesn't work. Also the child process might not be able to deal with such an exit command = at all for one reason or another so the only safe way to get rid of it is f= or the parent to kill it. The better option would be to not use a shared queue for communication and = instead use only dedicated pipes/queues for each child process but the does= n't seem to be a way to wait for a message from multiple queues/pipes. If t= hat were the case then I could simply kill the child and get rid of the res= pective pipes/queues without affecting the other processes or communication= channels. Regards, Dennis