Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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; 'case.': 0.05; 'discard': 0.05; 'socket': 0.05; '[],': 0.07; 'exit': 0.07; 'level,': 0.07; 'subject:How': 0.09; '22,': 0.09; 'abort': 0.09; 'blocking': 0.09; 'fetch': 0.09; 'termination': 0.09; 'timeout': 0.09; 'timeout)': 0.09; 'to:addr:comp.lang.python': 0.09; 'unexpected': 0.09; 'cc:addr:python-list': 0.10; 'aug': 0.13; 'apache': 0.13; 'async': 0.16; 'event-driven': 0.16; 'fine.': 0.16; 'pipes': 0.16; 'poll': 0.16; 'ready:': 0.16; 'segfault': 0.16; 'shared.': 0.16; 'true:': 0.16; 'twisted': 0.16; 'wed,': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'solution.': 0.18; 'requests': 0.18; 'issue.': 0.20; 'respective': 0.20; 'import': 0.21; 'assuming': 0.22; 'http': 0.22; 'pipe': 0.22; 'wednesday,': 0.22; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'external': 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; 'looks': 0.26; 'setting': 0.26; 'leave': 0.26; 'select': 0.26; 'cc:addr:gmail.com': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'actual': 0.28; 'decide': 0.28; 'parent': 0.29; 'queue': 0.29; 'reporting': 0.29; 'class': 0.29; 'framework': 0.30; 'point': 0.31; 'file': 0.32; 'running': 0.32; 'could': 0.32; 'rid': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'child': 0.36; 'too': 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; 'nothing': 0.38; 'instead': 0.39; 'think': 0.40; 'most': 0.61; 'remove': 0.61; 'lower': 0.61; 'dedicated': 0.61; 'solve': 0.62; 'safe': 0.63; 'agent': 0.64; 'request.': 0.64; 'making': 0.64; 'august': 0.66; 'receive': 0.71; 'killing': 0.84; 'cooperative': 0.91; 'dennis': 0.91; 'received:209.85.212.56': 0.91 Newsgroups: comp.lang.python Date: Wed, 22 Aug 2012 19:28:23 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=93.228.171.93; posting-account=_b6A3QoAAABPwmSQu8QIyLK7idqo6rsL References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 93.228.171.93 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345688905 news.xs4all.nl 6890 [2001:888:2000:d::a6]:35357 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27692 On Wednesday, August 22, 2012 11:15:10 PM UTC+2, Ian wrote: > On Wed, Aug 22, 2012 at 1:40 PM, Dennis Jacobfeuerborn >=20 > wrote: >=20 > > I was thinking about something like that but the issue is that this rea= lly 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 w= ork that might block for a while so the get() trick doesn't work. >=20 >=20 >=20 > At a lower level, it is possible to poll on both the pipe and the >=20 > socket simultaneously. At this point though you might want to start >=20 > looking at an asynchronous or event-driven framework like twisted or >=20 > gevent. >=20 I was looking at twisted and while the Agent class would allow me to make a= sync request it doesn't seem to support setting a timeout or aborting the r= unning request. That's really the important part since the http request is = really the only thing that might block for a while. If I can make the reque= st asynchronously and abort it when I receive a QUIT command from the paren= t then this would pretty much solve the issue. >=20 > > Also the child process might not be able to deal with such an exit comm= and at all for one reason or another so the only safe way to get rid of it = is for the parent to kill it. >=20 >=20 >=20 > I think you mean that it is the most "reliable" way. In general, the >=20 > only "safe" way to cause a process to exit is the cooperative >=20 > approach, because it may otherwise leave external resources such as >=20 > file data in an unexpected state that could cause problems later. >=20 True but the child is doing nothing but making http requests and reporting = the result to the parent so killing the process shouldn't be too much of a = deal in this case. A segfault in an Apache worker process is very similar i= n that it's an uncontrolled termination of the process and that works out f= ine. >=20 > > 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 = doesn'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= respective pipes/queues without affecting the other processes or communica= tion channels. >=20 >=20 >=20 > Assuming that you're using a Unix system: >=20 >=20 >=20 > from select import select >=20 >=20 >=20 > while True: >=20 > ready, _, _ =3D select(pipes, [], [], timeout) >=20 > if not ready: >=20 > # process timeout >=20 > else: >=20 > for pipe in ready: >=20 > message =3D pipe.get() >=20 > # process message That looks like a workable solution. When I decide to kill a worker process= I can remove the pipe from the pipes list and discard it since it's not sh= ared. Regards, Dennis