Threading Pool Event()

Cliff Wells logiplex at qwest.net
Fri Jul 18 18:28:48 EDT 2003


On Fri, 2003-07-18 at 14:28, Graeme Matthew wrote:
> Aahz
> 
> Thanks, ive actually been using your OSCON slides which have helped a lot.
> So it technically correct that all worker threads in a thread pool are
> either doing some work  or polling the queue to find out if there is a job
> to process ?

They don't poll the queue, they block waiting on it until something is
available.

> eg: (pseudocodish :-))
> 
> def run(self):
> 
>     while 1:
> 
>         self.lock.acquire()
> 
>         if QueueManager.HasJob:
>             job = QueueManager.GetFreeJob()
>             __processJob(job)
> 
>         self.lock.release()
> 
>  def __processJob(self):
> 
>         blah blah blah .....

More like:

def run():
    while 1:
        job = queue.get() # this blocks until something is queue.put()
        _processjob(job)


Acquiring the locks isn't necessary and ill-advised.  The Queue itself
can be thought of as a type of locking mechanism, so you raise the
possibility of a deadlock condition by nesting locks (for instance if
you also put locks around the queue.put()).

Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726  (800) 735-0555






More information about the Python-list mailing list