gevent.queue – 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.

Changed in version 1.0: Queue(0) now means queue of infinite size, not a channel.

The classes in this module implement iterator protocol. Iterating over queue means repeatedly calling get until get returns StopIteration.

>>> queue = gevent.queue.Queue()
>>> queue.put(1)
>>> queue.put(2)
>>> queue.put(StopIteration)
>>> for item in queue:
...    print item
1
2
class gevent.queue.Queue(maxsize=None, items=None)

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.

copy()
qsize()

Return the size of the queue.

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.

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.

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.

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()
next()
class gevent.queue.PriorityQueue(maxsize=None, items=None)

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

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

class gevent.queue.LifoQueue(maxsize=None, items=None)

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

class gevent.queue.JoinableQueue(maxsize=None, items=None, unfinished_tasks=None)

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

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

join()

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.

class gevent.queue.Channel
balance None
qsize()
empty()
full()
put(item, block=True, timeout=None)
put_nowait(item)
get(block=True, timeout=None)
get_nowait()
next()
exception gevent.queue.Full

An alias for Queue.Full

exception gevent.queue.Empty

An alias for Queue.Empty

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.lock – Locking primitives