[Python-ideas] `__iter__` for queues?

Raymond Hettinger raymond.hettinger at gmail.com
Wed Jan 20 20:36:39 CET 2010

On Jan 20, 2010, at 3:59 AM, cool-RR wrote:

> On Wed, Jan 20, 2010 at 7:24 AM, Anh Hai Trinh <anh.hai.trinh at gmail.com> wrote:
> [...]
> I agree, however, that this should be left out of the stdlib API.
> --
> // aht
> Okay, but what about having an object like `Queue.dump` (or some other name instead of `dump`), which can be iterated? This will be similar to `dict.keys` and `dict.values`.

I think you need to abandon the notion of iteration
and instead focus on your original use case of
(destructively) pulling all of the items out of a queue.

Taking a cue (no pun intended) from the DB-API's
fetchone() and fetchall(), perhaps you're looking for
a getall() method?

    def get_all(self, block=True, timeout=None):
        """Remove and return all items from the queue.

        If optional args 'block' is true and 'timeout' is None (the default),
        block if necessary until an item is available. If 'timeout' is
        a positive number, it blocks at most 'timeout' seconds and raises
        the Empty exception if no item was available within that time.
        Otherwise ('block' is false), return an item if one is immediately
        available, else raise the Empty exception ('timeout' is ignored
        in that case).
            if not block:
                if not self._qsize():
                    raise Empty
            elif timeout is None:
                while not self._qsize():
            elif timeout < 0:
                raise ValueError("'timeout' must be a positive number")
                endtime = _time() + timeout
                while not self._qsize():
                    remaining = endtime - _time()
                    if remaining <= 0.0:
                        raise Empty
            result = []
            while self._qsize():
            return result

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100120/c4130bd7/attachment.html>

More information about the Python-ideas mailing list