Search code examples
pythonpython-2.7exceptionblockingqueuebusy-waiting

Python blocking with queue.get() without exceptions or busy waiting


I have the following thread, for where the queue q is empty most of the time:

def run(self):

    while True:
        if not self.exit_flag:
            items = self.q.get()
            q.work_it()
        else:
            return 0

If the exit flag is set, it's not likely to exit immediately, because it's probably currently blocking at

items = self.q.get()

If I put a timeout on it

items = self.q.get(True, 0.1)

I will often raise the Empty exception, since the queue is more often empty than not, using more resources than I'd like.

If I do busy waiting like

def run(self):

    while True:
        if not self.exit_flag:
            if not self.q.empty():
                items = self.q.get()
                q.work_it()
            time.sleep(0.1)
        else:
            return 0

Then I do busy waiting instead of using the blocking feature of Queue.get() which seems ugly. It seems like I'm missing the elegant solution of this problem here? Is there one or should I just use the busy waiting solution?


Solution

  • Instead of using an exit_flag flag, consider putting a special "shutdown" indicator in the queue. When the worker dequeues a shutdown indicator, have it recognize the shutdown indicator and shut down instead of continuing on.