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.
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
Changed in version 1.0: Queue(0)
now means queue of infinite size, not a channel. A DeprecationWarning
will be issued with this argument.
Queue
(maxsize=None, items=None)¶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.
Changed in version 1.1b3: Multiple greenlets that block on a call to put()
for a full queue
will now be woken up 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
()¶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
()¶PriorityQueue
(maxsize=None, items=None)¶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)
.
LifoQueue
(maxsize=None, items=None)¶Bases: gevent.queue.Queue
A subclass of Queue
that retrieves most recently added entries first.
JoinableQueue
(maxsize=None, items=None, unfinished_tasks=None)¶Bases: gevent.queue.Queue
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
(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 . |
Channel
¶Bases: object
balance
¶qsize
()¶empty
()¶full
()¶put
(item, block=True, timeout=None)¶put_nowait
(item)¶get
(block=True, timeout=None)¶get_nowait
()¶next
()¶Full
¶An alias for Queue.Full
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.subprocess
– Cooperative subprocess
module