Threading Pool Event()

Tim Peters at
Sat Jul 19 00:29:47 CEST 2003

[Graeme Matthew, to 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 in the sense most people use that word:

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

I agree that's what most people mean by polling:  running a "spin loop",
checking over and over again, possibly with a time.sleep() call per
iteration to avoid saturating the CPU.  That's not what Queue.get() does,
though!  You can find the source code in Lib/  When .get() is
called without a timeout argument, it simply tries to acquire a particular
internal lock.  If you dig into that deeply enough, you'll eventually find
that if the thread can't acquire the lock immediately, in the bowels of your
C library and/or operating system it goes to sleep, and passively waits for
a platform-specific gimmick to wake it up again when the lock it's waiting
for gets released.  No looping is involved (well, none at the Python
level -- your C library and/or OS may have an internal loop, although in no
case should a thread waiting on a Queue.get() consume appreciable CPU time).

More information about the Python-list mailing list