Commit 8c903808 authored by Denis Bilenko's avatar Denis Bilenko

queue: implement iterator protocol in Queue

parent e82ccea9
......@@ -13,6 +13,18 @@ to :meth:`Queue.get` retrieves the item.
Another interesting difference is that :meth:`Queue.qsize`, :meth:`Queue.empty`, and
:meth:`Queue.full` *can* be used as indicators of whether the subsequent :meth:`Queue.get`
or :meth:`Queue.put` will not block.
Additionally, queues in this module implement iterator protocol. Iterating over queue
means repeatedly calling :meth:`get <Queue.get>` until :meth:`get <Queue.get>` returns ``StopIteration``.
>>> queue = gevent.queue.Queue()
>>> queue.put(1)
>>> queue.put(2)
>>> queue.put(StopIteration)
>>> for item in queue:
... print item
1
2
"""
import sys
......@@ -231,6 +243,15 @@ class Queue(object):
self._event_unlock = core.active_event(self._unlock)
# QQQ re-activate event (with event_active libevent call) instead of creating a new one each time
def __iter__(self):
return self
def next(self):
result = self.get()
if result is StopIteration:
raise result
return result
class ItemWaiter(Waiter):
__slots__ = ['item']
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment