Skip to content

Commit 0da1d29

Browse files
committed
asyncio: document Queue classes
1 parent 60efaae commit 0da1d29

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Doc/library/asyncio.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,114 @@ Coroutines (and tasks) can only run when the event loop is running.
901901
Synchronization primitives
902902
--------------------------
903903

904+
.. class:: Queue(maxsize=0, \*, loop=None)
905+
906+
A queue, useful for coordinating producer and consumer coroutines.
907+
908+
If *maxsize* is less than or equal to zero, the queue size is infinite. If
909+
it is an integer greater than ``0``, then ``yield from put()`` will block
910+
when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
911+
912+
Unlike the standard library :mod:`queue`, you can reliably know this Queue's
913+
size with :meth:`qsize`, since your single-threaded Tulip application won't
914+
be interrupted between calling :meth:`qsize` and doing an operation on the
915+
Queue.
916+
917+
.. method:: empty()
918+
919+
Return ``True`` if the queue is empty, ``False`` otherwise.
920+
921+
.. method:: full()
922+
923+
Return ``True`` if there are maxsize items in the queue.
924+
925+
.. note::
926+
927+
If the Queue was initialized with ``maxsize=0`` (the default), then
928+
:meth:`full()` is never ``True``.
929+
930+
.. method:: get()
931+
932+
Remove and return an item from the queue.
933+
934+
If you yield from :meth:`get()`, wait until a item is available.
935+
936+
This method returns a :ref:`coroutine <coroutine>`.
937+
938+
.. method:: get_nowait()
939+
940+
Remove and return an item from the queue.
941+
942+
Return an item if one is immediately available, else raise
943+
:exc:`~queue.Empty`.
944+
945+
.. method:: put(item)
946+
947+
Put an item into the queue.
948+
949+
If you yield from ``put()``, wait until a free slot is available before
950+
adding item.
951+
952+
This method returns a :ref:`coroutine <coroutine>`.
953+
954+
.. method:: put_nowait(item)
955+
956+
Put an item into the queue without blocking.
957+
958+
If no free slot is immediately available, raise :exc:`~queue.Full`.
959+
960+
.. method:: qsize()
961+
962+
Number of items in the queue.
963+
964+
.. attribute:: maxsize
965+
966+
Number of items allowed in the queue.
967+
968+
.. class:: PriorityQueue
969+
970+
A subclass of :class:`Queue`; retrieves entries in priority order (lowest
971+
first).
972+
973+
Entries are typically tuples of the form: (priority number, data).
974+
975+
.. class:: LifoQueue
976+
977+
A subclass of :class:`Queue` that retrieves most recently added entries
978+
first.
979+
980+
.. class:: JoinableQueue
981+
982+
A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join`
983+
methods.
984+
985+
.. method:: task_done()
986+
987+
Indicate that a formerly enqueued task is complete.
988+
989+
Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
990+
subsequent call to :meth:`task_done` tells the queue that the processing
991+
on the task is complete.
992+
993+
If a :meth:`join` is currently blocking, it will resume when all items
994+
have been processed (meaning that a :meth:`task_done` call was received
995+
for every item that had been :meth:`~Queue.put` into the queue).
996+
997+
Raises :exc:`ValueError` if called more times than there were items
998+
placed in the queue.
999+
1000+
.. method:: join()
1001+
1002+
Block until all items in the queue have been gotten and processed.
1003+
1004+
The count of unfinished tasks goes up whenever an item is added to the
1005+
queue. The count goes down whenever a consumer thread calls
1006+
:meth:`task_done` to indicate that the item was retrieved and all work on
1007+
it is complete. When the count of unfinished tasks drops to zero,
1008+
:meth:`join` unblocks.
1009+
1010+
This method returns a :ref:`coroutine <coroutine>`.
1011+
9041012

9051013
Examples
9061014
--------

0 commit comments

Comments
 (0)