Path: csiph.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'socket': 0.05; '[],': 0.07; 'exit': 0.07; 'level,': 0.07; 'subject:How': 0.09; '22,': 0.09; 'blocking': 0.09; 'cc:addr:googlegroups.com': 0.09; 'fetch': 0.09; 'received:mail-lpp01m010-f46.google.com': 0.09; 'timeout': 0.09; 'timeout)': 0.09; 'unexpected': 0.09; 'cc:addr:python-list': 0.10; 'aug': 0.13; 'event-driven': 0.16; 'poll': 0.16; 'ready:': 0.16; 'true:': 0.16; 'twisted': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'respective': 0.20; 'import': 0.21; 'assuming': 0.22; 'pipe': 0.22; 'work.': 0.23; 'external': 0.24; 'cc:2**1': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'leave': 0.26; 'select': 0.26; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'actual': 0.28; 'parent': 0.29; 'queue': 0.29; 'framework': 0.30; 'received:209.85.215.46': 0.30; 'point': 0.31; 'file': 0.32; 'could': 0.32; 'rid': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'child': 0.36; 'problems': 0.36; 'possible': 0.37; 'option': 0.37; 'resources': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'instead': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'most': 0.61; 'lower': 0.61; 'dedicated': 0.61; 'safe': 0.63; 'cooperative': 0.91; '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=PQyvy+OZiXbWhHojqy0pQqHF2f2ulYKvG63zDIEYR+0=; b=G+GBDjoQhczXBNd3JUbBj8mEq1RAMcQokIxj43/qeEkxGaL1SuHiRe1ZhXwFQpG50h 6qjGTPbrzEowQt7IOxgm3uSeNPVtTh0/JhT3ipHkNWK43bmNMSSuyJpxNEO7PyqFJ/nb nmkWcDpDjrYeXiRQfa9/7bGNH1uew5rNOYfDmDrKkzGCIjkEFKeYyJG/4xWYG8vWlyrh HQHly/CMfI0cVGsYhJkcVq0BUtFcysjsJjKV8IaKpeZC7O2B37yyCPIapSDhNj1VcdBQ RlTXREeoITlHisWs2/cgCQkju/mMJAUBMkog3TEZRifvUEJgk7IZHa8f1Nq81guTUTpH T8Fg== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 22 Aug 2012 15:15:10 -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, 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345670143 news.xs4all.nl 6876 [2001:888:2000:d::a6]:57602 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27685 On Wed, Aug 22, 2012 at 1:40 PM, Dennis Jacobfeuerborn wrote: > I was thinking about something like that but the issue is that this reall= y only works when you don't do any actual blocking work. I may be able to g= et around the sleep() but then I have to fetch the URL or do some other wor= k that might block for a while so the get() trick doesn't work. At a lower level, it is possible to poll on both the pipe and the socket simultaneously. At this point though you might want to start looking at an asynchronous or event-driven framework like twisted or gevent. > Also the child process might not be able to deal with such an exit comman= d at all for one reason or another so the only safe way to get rid of it is= for the parent to kill it. I think you mean that it is the most "reliable" way. In general, the only "safe" way to cause a process to exit is the cooperative approach, because it may otherwise leave external resources such as file data in an unexpected state that could cause problems later. > The better option would be to not use a shared queue for communication an= d instead use only dedicated pipes/queues for each child process but the do= esn't seem to be a way to wait for a message from multiple queues/pipes. If= that were the case then I could simply kill the child and get rid of the r= espective pipes/queues without affecting the other processes or communicati= on channels. Assuming that you're using a Unix system: from select import select while True: ready, _, _ =3D select(pipes, [], [], timeout) if not ready: # process timeout else: for pipe in ready: message =3D pipe.get() # process message