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.
Bases: Exception
Exception raised by Queue.get(block=0)/get_nowait().
Bases: 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.
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.
timeout (float) – If not None
, then wait no more than this time in seconds
for all tasks to finish.
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.
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.
Bases: Queue
A subclass of Queue
that retrieves most recently added entries first.
Bases: 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.
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: 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.
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).
Remove and return an item from the queue without blocking.
Only get an item if one is immediately available. Otherwise
raise the Empty
exception.
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).
Return an item from the queue without blocking.
Only return an item if one is immediately available. Otherwise
raise the Empty
exception.
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 an item into the queue without blocking.
Only enqueue the item if a free slot is immediately available.
Otherwise raise the Full
exception.
alias of _PySimpleQueue
An alias for Queue.Full
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.local
– Greenlet-local objects