# gevent

# `gevent.queue` – Synchronized queues

Synchronized queues.

The `gevent.queue` module implements multi-producer, multi-consumer queues that work across greenlets, with the API similar to the classes found in the standard `Queue` and `multiprocessing` modules.

The classes in this module implement the iterator protocol. Iterating over a queue means repeatedly calling `get` until `get` returns `StopIteration` (specifically that class, not an instance or subclass).

    >>> import gevent.queue
    >>> queue = gevent.queue.Queue()
    >>> queue.put(1)
    >>> queue.put(2)
    >>> queue.put(StopIteration)
    >>> for item in queue:
    ...    print(item)
    1
    2

Changed in version 1.0: `Queue(0)` now means queue of infinite size, not a channel. A `DeprecationWarning` will be issued with this argument.

exception Empty

  Bases: `Exception`

  Exception raised by Queue.get(block=0)/get_nowait().

exception Full

  Bases: `Exception`

  Exception raised by Queue.put(block=0)/put_nowait().

class Channel(maxsize=1)

  Bases: `object`

  empty()
  full()
  get(block=True, timeout=None)
  get_nowait()
  next()
  put(item, block=True, timeout=None)
  put_nowait(item)
  qsize()
  property balance
  getters
  hub
  putters

class JoinableQueue(maxsize=None, items=(), unfinished_tasks=None)

  Bases: `gevent.queue.Queue`

  A subclass of `Queue` that additionally has `task_done()` and `join()` methods.

  Changed in version 1.1a1: If unfinished_tasks is not given, then all the given items (if any) will be considered unfinished.

  copy()
  join(timeout=None)

    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 thread 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.

    Parameters

      timeout (float) – If not `None`, then wait no more than this time in seconds for all tasks to finish.

    Returns

      `True` if all tasks have finished; if `timeout` was given and expired before all tasks finished, `False`.

    Changed in version 1.1a1: Add the timeout parameter.

  task_done()

    Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each `get` used to fetch a task, a subsequent call to `task_done()` tells the queue that the processing on the task is complete.

    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 a `ValueError` if called more times than there were items placed in the queue.

  unfinished_tasks

class LifoQueue(maxsize=None, items=(), _warn_depth=2)

  Bases: `gevent.queue.Queue`

  A subclass of `Queue` that retrieves most recently added entries first.

class PriorityQueue(maxsize=None, items=(), _warn_depth=2)

  Bases: `gevent.queue.Queue`

  A subclass of `Queue` that retrieves entries in priority order (lowest first).

  Entries are typically tuples of the form: `(priority number, data)`.

  Changed in version 1.2a1: Any items given to the constructor will now be passed through `heapq.heapify()` to ensure the invariants of this class hold. Previously it was just assumed that they were already a heap.

class Queue(maxsize=None, items=(), _warn_depth=2)

  Bases: `object`

  Create a queue object with a given maximum size.

  If maxsize is less than or equal to zero or `None`, the queue size is infinite.

  Queues have a `len` equal to the number of items in them (the `qsize()`), but in a boolean context they are always True.

  Changed in version 1.1b3: Queues now support `len()`; it behaves the same as `qsize()`.

  Changed in version 1.1b3: Multiple greenlets that block on a call to `put()` for a full queue will now be awakened to put their items into the queue in the order in which they arrived. Likewise, multiple greenlets that block on a call to `get()` for an empty queue will now receive items in the order in which they blocked. An implementation quirk under CPython usually ensured this was roughly the case previously anyway, but that wasn’t the case for PyPy.

  copy()
  empty()

    Return `True` if the queue is empty, `False` otherwise.

  full()

    Return `True` if the queue is full, `False` otherwise.

    `Queue(None)` is never full.

  get(block=True, timeout=None)

    Remove and return an item 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).

  get_nowait()

    Remove and return an item from the queue without blocking.

    Only get an item if one is immediately available. Otherwise raise the `Empty` exception.

  next()
  peek(block=True, timeout=None)

    Return an item from the queue without removing it.

    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).

  peek_nowait()

    Return an item from the queue without blocking.

    Only return an item if one is immediately available. Otherwise raise the `Empty` exception.

  put(item, block=True, timeout=None)

    Put an item into the queue.

    If optional arg block is true and timeout is `None` (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the `Full` exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the `Full` exception (timeout is ignored in that case).

  put_nowait(item)

    Put an item into the queue without blocking.

    Only enqueue the item if a free slot is immediately available. Otherwise raise the `Full` exception.

  qsize()

    Return the size of the queue.

  getters
  hub
  property maxsize
  putters
  queue

SimpleQueue

  alias of `queue._PySimpleQueue`

exception Full

  An alias for `Queue.Full`

exception Empty

  An alias for `Queue.Empty`

## Examples

Example of how to wait for enqueued tasks to be completed:

    def worker():
        while True:
            item = q.get()
            try:
                do_work(item)
            finally:
                q.task_done()

    q = JoinableQueue()
    for i in range(num_worker_threads):
         gevent.spawn(worker)

    for item in source():
        q.put(item)

    q.join()  # block until all tasks are done

Next page: `gevent.local` – Greenlet-local objects