Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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; 'args': 0.04; 'data:': 0.07; 'exit': 0.07; 'try:': 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spawn': 0.09; 'stdout': 0.09; 'tends': 0.09; 'threads,': 0.09; 'unexpected': 0.09; 'def': 0.10; 'thread': 0.11; 'aug': 0.13; '[and': 0.16; 'needed?': 0.16; 'poll': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'threading': 0.16; 'threading,': 0.16; 'threads': 0.16; 'time-out': 0.16; 'true:': 0.16; 'url)': 0.16; 'wed,': 0.16; 'systems.': 0.18; 'form:': 0.22; 'pipe': 0.22; 'command': 0.24; 'pass': 0.25; 'setting': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.26; 'checking': 0.27; 'plain': 0.27; 'header:X -Complaints-To:1': 0.28; 'rest': 0.28; 'actual': 0.28; 'fine': 0.28; '"do': 0.29; 'efficiently': 0.29; 'i/o': 0.29; 'overhead': 0.29; 'queue': 0.29; 'remains': 0.29; 'wrap': 0.29; 'probably': 0.29; 'connections': 0.30; 'could': 0.32; 'true.': 0.33; 'url:home': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'operations': 0.33; 'point.': 0.33; 'done': 0.34; 'third': 0.34; 'whatever': 0.35; 'collecting': 0.35; 'false': 0.35; 'same.': 0.35; 'doing': 0.35; 'next': 0.35; 'received:org': 0.36; 'ability': 0.36; 'except': 0.36; 'but': 0.36; 'child': 0.36; 'level.': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'step': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'remove': 0.61; 'results': 0.65; 'reverse': 0.65; 'sound': 0.65; 'receive': 0.71; 'actually,': 0.84; 'check)': 0.84; 'to/from': 0.84; 'controller': 0.91; 'dennis': 0.91; 'received:108': 0.91; 'processes,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: How to properly implement worker processes Date: Wed, 22 Aug 2012 16:09:56 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-108-79-218-57.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES 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: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345666196 news.xs4all.nl 6920 [2001:888:2000:d::a6]:42480 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27682 On Wed, 22 Aug 2012 10:29:49 -0700 (PDT), Dennis Jacobfeuerborn declaimed the following in gmane.comp.python.general: > How can I get around this problem and receive status updates from all children efficiently without a shared queue and with the ability to simply kill the child process when it's no longer needed? > How much actual processing is done during the "check"? Your description makes it sound like these are I/O bound operations (combined with sleep()) -- and plain old threading tends to work fine for I/O bound systems. If you used threading, you could signal a thread to die via simply setting a property in the thread: t[x].die = True. The thread would wrap a loop of the form: while not self.die: #do URL check resque.put(status of check) time.sleep() Thereby doing away with many of your queues -- you'd only need the result/status queue, and the thread would only exit at a clean point. The next step up, depending on the overhead of spawning processes, would be to still use control threads and a local result queue, but have "do URL check" create the check process each time -- you could probably use "proc.communicate()" to obtain the status via the process stdout [and pass the URL via stdin]. The rest of the control thread remains the same. The third step: Still use controller threads, but the controller thread would create a queue pair (to-process, from-process) on initialization, and then spawn the process. You might even be able to remove the time.sleep() from the thread level. Actually, checking the docs, forget about the Queue... Use a Pipe self.from, self.to = multiprocessing.Pipe() self.p = multiprocessing.Process(target = worker, args = (self.from, self.to, URL) ) workerdead = False while not self.die: try: status = self.from.recv() #blocks until data resque.put(status) #local Queue collecting results except EOFError: # whatever for unexpected shutdown workerdead = True if not workerdead: self.to.send("SHUTDOWN") while True: status = self.from.recv() if status == "SHUTTING DOWN": break resque.put(status) #might have had a last cycle The worker should poll rather than sleep. def worker(pto, pfrom, URL): #note reverse of to/from connections while True: #do URL check pto.send(status) data = pfrom.poll(1.0) #sleep until command or time-out if data: command = pfrom.recv() if command == "SHUTDOWN": pto.send("SHUTTING DOWN") break -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/