__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty']
from .coroutines import coroutine
class QueueEmpty(Exception):
"""Exception raised when Queue.get_nowait() is called on a Queue object
class QueueFull(Exception):
"""Exception raised when the Queue.put_nowait() method is called on a Queue
"""A queue, useful for coordinating producer and consumer coroutines.
If maxsize is less than or equal to zero, the queue size is infinite. If it
is an integer greater than 0, then "yield from put()" will block when the
queue reaches maxsize, until an item is removed by get().
Unlike the standard library Queue, you can reliably know this Queue's size
with qsize(), since your single-threaded asyncio application won't be
interrupted between calling qsize() and doing an operation on the Queue.
def __init__(self, maxsize=0, *, loop=None):
self._loop = events.get_event_loop()
self._getters = collections.deque()
self._putters = collections.deque()
self._unfinished_tasks = 0
self._finished = locks.Event(loop=self._loop)
# These three are overridable in subclasses.
def _init(self, maxsize):
self._queue = collections.deque()
return self._queue.popleft()
# End of the overridable methods.
def _wakeup_next(self, waiters):
# Wake up the next waiter (if any) that isn't cancelled.
waiter = waiters.popleft()
return '<{} at {:#x} {}>'.format(
type(self).__name__, id(self), self._format())
return '<{} {}>'.format(type(self).__name__, self._format())
result = 'maxsize={!r}'.format(self._maxsize)
if getattr(self, '_queue', None):
result += ' _queue={!r}'.format(list(self._queue))
result += ' _getters[{}]'.format(len(self._getters))
result += ' _putters[{}]'.format(len(self._putters))
if self._unfinished_tasks:
result += ' tasks={}'.format(self._unfinished_tasks)
"""Number of items in the queue."""
"""Number of items allowed in the queue."""
"""Return True if the queue is empty, False otherwise."""
"""Return True if there are maxsize items in the queue.
Note: if the Queue was initialized with maxsize=0 (the default),
then full() is never True.
return self.qsize() >= self._maxsize
"""Put an item into the queue.
Put an item into the queue. If the queue is full, wait until a free
slot is available before adding item.
This method is a coroutine.
putter = self._loop.create_future()
self._putters.append(putter)
putter.cancel() # Just in case putter is not done yet.
if not self.full() and not putter.cancelled():
# We were woken up by get_nowait(), but can't take
# the call. Wake up the next in line.
self._wakeup_next(self._putters)
return self.put_nowait(item)
def put_nowait(self, item):
"""Put an item into the queue without blocking.
If no free slot is immediately available, raise QueueFull.
self._unfinished_tasks += 1
self._wakeup_next(self._getters)
"""Remove and return an item from the queue.
If queue is empty, wait until an item is available.
This method is a coroutine.
getter = self._loop.create_future()
self._getters.append(getter)
getter.cancel() # Just in case getter is not done yet.
self._getters.remove(getter)
if not self.empty() and not getter.cancelled():
# We were woken up by put_nowait(), but can't take
# the call. Wake up the next in line.
self._wakeup_next(self._getters)
"""Remove and return an item from the queue.
Return an item if one is immediately available, else raise QueueEmpty.
self._wakeup_next(self._putters)
"""Indicate that a formerly enqueued task is complete.
Used by queue consumers. For each get() used to fetch a task,
a subsequent call to task_done() tells the queue that the processing
If a join() is currently blocking, it will resume when all items have
been processed (meaning that a task_done() call was received for every
item that had been put() into the queue).
Raises ValueError if called more times than there were items placed in
if self._unfinished_tasks <= 0:
raise ValueError('task_done() called too many times')
self._unfinished_tasks -= 1
if self._unfinished_tasks == 0:
"""Block until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer calls task_done() to
indicate that the item was retrieved and all work on it is complete.
When the count of unfinished tasks drops to zero, join() unblocks.
if self._unfinished_tasks > 0:
yield from self._finished.wait()
class PriorityQueue(Queue):
"""A subclass of Queue; retrieves entries in priority order (lowest first).
Entries are typically tuples of the form: (priority number, data).
def _init(self, maxsize):
def _put(self, item, heappush=heapq.heappush):
heappush(self._queue, item)
def _get(self, heappop=heapq.heappop):
return heappop(self._queue)
"""A subclass of Queue that retrieves most recently added entries first."""
def _init(self, maxsize):
"""Deprecated alias for Queue."""
__all__.append('JoinableQueue')