Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python iterable Queue

I need to know when a Queue is closed and wont have more items so I can end the iteration.

I did it by putting a sentinel in the queue:

from Queue import Queue

class IterableQueue(Queue): 

    _sentinel = object()

    def __iter__(self):
        return self

    def close(self):
        self.put(self._sentinel)

    def next(self):
        item = self.get()
        if item is self._sentinel:
            raise StopIteration
        else:
            return item

Given that this is a very common use for a queue, isn't there any builtin implementation?

like image 816
pomber Avatar asked Jul 02 '12 05:07

pomber


People also ask

Can you iterate through a queue Python?

Use a while loop to iterate through a queue in Python, e.g. while not q. empty(): . The loop checks if the queue is not empty and iterates as long as there are items in the queue.

What is queue queue () in Python?

A queue is a collection of objects that supports fast first-in, first-out (FIFO) semantics for inserts and deletes. The insert and delete operations sometimes called enqueue and dequeue. Unlike lists or arrays, queues typically don't allow for random access to the objects they contain.

Is Python queue get blocking?

Yep, queue. get() will block only a thread where it was called.


1 Answers

A sentinel is a reasonable way for a producer to send a message that no more queue tasks are forthcoming.

FWIW, your code can be simplified quite a bit with the two argument form of iter():

from Queue import Queue

class IterableQueue(Queue): 

    _sentinel = object()

    def __iter__(self):
        return iter(self.get, self._sentinel)

    def close(self):
        self.put(self._sentinel)
like image 187
Raymond Hettinger Avatar answered Sep 18 '22 17:09

Raymond Hettinger