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.
- class JoinableQueue(maxsize=None, items=(), unfinished_tasks=None)[source]¶
Bases:
QueueA subclass of
Queuethat additionally hastask_done()andjoin()methods.Changed in version 1.1a1: If unfinished_tasks is not given, then all the given items (if any) will be considered unfinished.
- join(timeout=None)[source]¶
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:
Trueif all tasks have finished; iftimeoutwas given and expired before all tasks finished,False.
Changed in version 1.1a1: Add the timeout parameter.
- task_done()[source]¶
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each
getused to fetch a task, a subsequent call totask_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 atask_done()call was received for every item that had beenputinto the queue).Raises a
ValueErrorif called more times than there were items placed in the queue.
- unfinished_tasks¶
- class LifoQueue(maxsize=None, items=(), unfinished_tasks=None)[source]¶
Bases:
JoinableQueueA subclass of
JoinableQueuethat retrieves most recently added entries first.Changed in version 24.10.1: Now extends
JoinableQueueinstead of justQueue.Changed in version 1.1a1: If unfinished_tasks is not given, then all the given items (if any) will be considered unfinished.
- class PriorityQueue(maxsize=None, items=(), _warn_depth=2)[source]¶
Bases:
QueueA subclass of
Queuethat 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)[source]¶
Bases:
objectCreate 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
lenequal to the number of items in them (theqsize()), but in a boolean context they are always True.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 toget()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.Changed in version 24.10.1: Implement the
shutdownmethods from Python 3.13.- get(block=True, timeout=None)[source]¶
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 theEmptyexception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise theEmptyexception (timeout is ignored in that case).
- get_nowait()[source]¶
Remove and return an item from the queue without blocking.
Only get an item if one is immediately available. Otherwise raise the
Emptyexception.
- peek(block=True, timeout=None)[source]¶
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 theEmptyexception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise theEmptyexception (timeout is ignored in that case).
- peek_nowait()[source]¶
Return an item from the queue without blocking.
Only return an item if one is immediately available. Otherwise raise the
Emptyexception.
- put(item, block=True, timeout=None)[source]¶
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 theFullexception 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 theFullexception (timeout is ignored in that case).- … versionchanged:: 24.10.1
Now raises a
ValueErrorfor a negative timeout in the cases that CPython does.
- put_nowait(item)[source]¶
Put an item into the queue without blocking.
Only enqueue the item if a free slot is immediately available. Otherwise raise the
Fullexception.
- shutdown(immediate=False)[source]¶
“Shut-down the queue, making queue gets and puts raise
ShutDown.By default, gets will only raise once the queue is empty. Set immediate to True to make gets raise immediately instead.
All blocked callers of
putandgetwill be unblocked.In joinable queues, if immediate, a task is marked as done for each item remaining in the queue, which may unblock callers of
join.
- getters¶
- hub¶
- is_shutdown¶
- property maxsize¶
- putters¶
- queue¶
- SimpleQueue¶
alias of
_PySimpleQueue
- 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