An improvement to Queue

Aahz Maruch aahz at panix.com
Thu Jan 31 16:22:23 EST 2002


In article <Xns91A7A14C232EBmichaelrcpcouk at 194.238.50.13>,
Michael Abbott  <michael at rcp.co.uk> wrote:
>
>I have a worker thread which sits in a tight loop doing the following:
>
>    	1. Waiting on an event
>    	2. Doing some work
>
>This can be implemented using the existing Queue as simply (this isn't 
>actually how I do it, but the intent is the same):
>
>    	while 1:
>    	    (action_queue.get())()
>
>This thread in general will only block on the action_queue.get() call 
>
>Other threads are busily running around in the background preparing work 
>for the worker thread to work on.  Here's a typical background activity:
>
>    	on interesting_event:
>    	    if my_queue.put(some_work):    	    	# This is the key line
>    	        action_queue.put(do_the_work)
>
>    	def do_the_work():
>    	    process_work(my_queue.get_all())
>
>The idea here is that until the worker thread is ready I don't want to call 
>process_work, indeed process_work is supposed to happen on the worker 
>thread, but when it is ready it should process everything that's in hand.
>
>
>Let's be more explicit.
>
>An interesting event might be a socket read ready event: I read what I can 
>from the socket, and queue the results for processing on the worker thread.  
>I don't particularly want to queue a separate do_the_work event for each 
>incoming block, particularly as the worker thread might be really quite 
>busy at times (in fact, at the moment I'm running 100% cpu for minutes at a 
>stretch; Python has its challenges).

What I still don't get is why you're waiting on an event rather than on
data.  It seems pointless.  Just have the worker thread grab data and
process it.
-- 
                      --- Aahz  <*>  (Copyright 2002 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista   

"The more you drive, the less intelligent you are."  --_Repo Man_



More information about the Python-list mailing list