+ def _WaitForTaskUnlocked(self, worker):
+ """Waits for a task for a worker.
+
+ @type worker: L{BaseWorker}
+ @param worker: Worker thread
+
+ """
+ if self._ShouldWorkerTerminateUnlocked(worker):
+ return _TERMINATE
+
+ # We only wait if there's no task for us.
+ if not (self._active and self._tasks):
+ logging.debug("Waiting for tasks")
+
+ while True:
+ # wait() releases the lock and sleeps until notified
+ self._pool_to_worker.wait()
+
+ logging.debug("Notified while waiting")
+
+ # Were we woken up in order to terminate?
+ if self._ShouldWorkerTerminateUnlocked(worker):
+ return _TERMINATE
+
+ # Just loop if pool is not processing tasks at this time
+ if self._active and self._tasks:
+ break
+
+ # Get task from queue and tell pool about it
+ try:
+ return heapq.heappop(self._tasks)
+ finally:
+ self._worker_to_pool.notifyAll()
+
+ def _ShouldWorkerTerminateUnlocked(self, worker):
+ """Returns whether a worker should terminate.
+
+ """
+ return (worker in self._termworkers)
+