False Queue.Empty exception ?

Scott Blomquist blomquist_scott at hotmail.com
Mon Jun 10 16:34:47 EDT 2002


I use the Queue class to maintain a threadpool of threads which
currently have no task, rather than creating/reaping threads as they
are needed. When I have a task, I grab one out of the queue and assign
the task to it. To add complexity :) I have made the threadpool able
to have a min and a max number of threads created, and if the queue of
free threads is Empty, then I (potentially) create more threads (up to
the max), and drop them into the free queue.

The problem is that the Empty exception appears to be raised even when
the queue is in fact not empty - causing me to create threads when I
don't really need to - some sort of race condition in the following
code from the Queue class?

    def get(self, block=1):
        """Remove and return an item from the queue.

        If optional arg 'block' is 1 (the default), block if
        necessary until an item is available.  Otherwise (block is 0),
        return an item if one is immediately available, else raise the
        Empty exception.
        """
        if block:
            self.esema.acquire()
        elif not self.esema.acquire(0):
            raise Empty
        self.mutex.acquire()
        was_full = self._full()
        item = self._get()
        if was_full:
            self.fsema.release()
        if not self._empty():
            self.esema.release()
        self.mutex.release()
        return item

Keeping in mind that I am doing a non-blocking get, it seems as though
if there is a thread between the self.esema.acquire(0) and the
self.esema.release(), and it gets switched out and another thread is
switched in and comes through the get call, it will fail on the
self.esema.acquire(0), even though the queue is not neccessarily
emtpy.

Can someone help me out here? Am I just misusing/misunderstanding the
capabilities of Queue class?

Thanks,
--ScottB



More information about the Python-list mailing list