Skip to content

Commit 66efd9c

Browse files
authored
Merge pull request #4 from EpicWink/yvesdup-queue-shutdown-2
Update docs for queue shutdown
2 parents 49879a0 + 9eed14e commit 66efd9c

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

Doc/library/asyncio-queue.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Queue
6262
Remove and return an item from the queue. If queue is empty,
6363
wait until an item is available.
6464

65+
Raises :exc:`QueueShutDown` if the queue has been shut down and
66+
is empty, or if the queue has been shut down immediately.
67+
6568
.. method:: get_nowait()
6669

6770
Return an item if one is immediately available, else raise
@@ -77,11 +80,16 @@ Queue
7780
work on it is complete. When the count of unfinished tasks drops
7881
to zero, :meth:`join` unblocks.
7982

83+
Raises :exc:`QueueShutDown` if the queue has been shut down
84+
immediately.
85+
8086
.. coroutinemethod:: put(item)
8187

8288
Put an item into the queue. If the queue is full, wait until a
8389
free slot is available before adding the item.
8490

91+
Raises :exc:`QueueShutDown` if the queue has been shut down.
92+
8593
.. method:: put_nowait(item)
8694

8795
Put an item into the queue without blocking.
@@ -92,6 +100,19 @@ Queue
92100

93101
Return the number of items in the queue.
94102

103+
.. method:: shutdown(immediate=False)
104+
105+
Shut-down the queue, making queue gets and puts raise
106+
:exc:`QueueShutDown`.
107+
108+
By default, gets will only raise once the queue is empty. Set
109+
*immediate* to true to make gets raise immediately instead.
110+
111+
All blocked callers of put() will be unblocked, and also get()
112+
and join() if *immediate* is true.
113+
114+
.. versionadded:: 3.13
115+
95116
.. method:: task_done()
96117

97118
Indicate that a formerly enqueued task is complete.
@@ -108,6 +129,9 @@ Queue
108129
Raises :exc:`ValueError` if called more times than there were
109130
items placed in the queue.
110131

132+
Raises :exc:`QueueShutDown` if the queue has been shut down
133+
immediately.
134+
111135

112136
Priority Queue
113137
==============
@@ -145,6 +169,14 @@ Exceptions
145169
on a queue that has reached its *maxsize*.
146170

147171

172+
.. exception:: QueueShutDown
173+
174+
Exception raised when getting an item from or putting an item onto a
175+
queue which has been shut down.
176+
177+
.. versionadded:: 3.13
178+
179+
148180
Examples
149181
========
150182

Doc/library/multiprocessing.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,8 @@ For an example of the usage of queues for interprocess communication see
847847
free slot was available within that time. Otherwise (*block* is
848848
``False``), put an item on the queue if a free slot is immediately
849849
available, else raise the :exc:`queue.Full` exception (*timeout* is
850-
ignored in that case).
850+
ignored in that case). Raises :exc:`ShutDown` if the queue has been shut
851+
down.
851852

852853
.. versionchanged:: 3.8
853854
If the queue is closed, :exc:`ValueError` is raised instead of
@@ -865,7 +866,9 @@ For an example of the usage of queues for interprocess communication see
865866
it blocks at most *timeout* seconds and raises the :exc:`queue.Empty`
866867
exception if no item was available within that time. Otherwise (block is
867868
``False``), return an item if one is immediately available, else raise the
868-
:exc:`queue.Empty` exception (*timeout* is ignored in that case).
869+
:exc:`queue.Empty` exception (*timeout* is ignored in that case). Raises
870+
:exc:`queue.ShutDown` if the queue has been shut down and is empty, or if
871+
the queue has been shut down immediately.
869872

870873
.. versionchanged:: 3.8
871874
If the queue is closed, :exc:`ValueError` is raised instead of
@@ -875,6 +878,19 @@ For an example of the usage of queues for interprocess communication see
875878

876879
Equivalent to ``get(False)``.
877880

881+
.. method:: shutdown(immediate=False)
882+
883+
Shut-down the queue, making queue gets and puts raise
884+
:exc:`queue.ShutDown`.
885+
886+
By default, gets will only raise once the queue is empty. Set
887+
*immediate* to true to make gets raise immediately instead.
888+
889+
All blocked callers of put() will be unblocked, and also get()
890+
and join() if *immediate* is true.
891+
892+
.. versionadded:: 3.13
893+
878894
:class:`multiprocessing.Queue` has a few additional methods not found in
879895
:class:`queue.Queue`. These methods are usually unnecessary for most
880896
code:
@@ -964,6 +980,8 @@ For an example of the usage of queues for interprocess communication see
964980
Raises a :exc:`ValueError` if called more times than there were items
965981
placed in the queue.
966982

983+
Raises :exc:`queue.ShutDown` if the queue has been shut down immediately.
984+
967985

968986
.. method:: join()
969987

@@ -975,6 +993,8 @@ For an example of the usage of queues for interprocess communication see
975993
it is complete. When the count of unfinished tasks drops to zero,
976994
:meth:`~queue.Queue.join` unblocks.
977995

996+
Raises :exc:`queue.ShutDown` if the queue has been shut down immediately.
997+
978998

979999
Miscellaneous
9801000
~~~~~~~~~~~~~

Doc/library/queue.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ The :mod:`queue` module defines the following classes and exceptions:
9393
on a :class:`Queue` object which is full.
9494

9595

96+
.. exception:: ShutDown
97+
98+
Exception raised when :meth:`~Queue.put` or :meth:`~Queue.get` is called on
99+
a :class:`Queue` object which has been shut down.
100+
101+
.. versionadded:: 3.13
102+
103+
96104
.. _queueobjects:
97105

98106
Queue Objects
@@ -135,6 +143,8 @@ provide the public methods described below.
135143
immediately available, else raise the :exc:`Full` exception (*timeout* is
136144
ignored in that case).
137145

146+
Raises :exc:`ShutDown` if the queue has been shut down.
147+
138148

139149
.. method:: Queue.put_nowait(item)
140150

@@ -155,6 +165,9 @@ provide the public methods described below.
155165
an uninterruptible wait on an underlying lock. This means that no exceptions
156166
can occur, and in particular a SIGINT will not trigger a :exc:`KeyboardInterrupt`.
157167

168+
Raises :exc:`ShutDown` if the queue has been shut down and is empty, or if
169+
the queue has been shut down immediately.
170+
158171

159172
.. method:: Queue.get_nowait()
160173

@@ -177,6 +190,8 @@ fully processed by daemon consumer threads.
177190
Raises a :exc:`ValueError` if called more times than there were items placed in
178191
the queue.
179192

193+
Raises :exc:`ShutDown` if the queue has been shut down immediately.
194+
180195

181196
.. method:: Queue.join()
182197

@@ -187,6 +202,8 @@ fully processed by daemon consumer threads.
187202
indicate that the item was retrieved and all work on it is complete. When the
188203
count of unfinished tasks drops to zero, :meth:`join` unblocks.
189204

205+
Raises :exc:`ShutDown` if the queue has been shut down immediately.
206+
190207

191208
Example of how to wait for enqueued tasks to be completed::
192209

@@ -214,6 +231,25 @@ Example of how to wait for enqueued tasks to be completed::
214231
print('All work completed')
215232

216233

234+
Terminating queues
235+
^^^^^^^^^^^^^^^^^^
236+
237+
:class:`Queue` objects can be made to prevent further interaction by shutting
238+
them down.
239+
240+
.. method:: Queue.shutdown(immediate=False)
241+
242+
Shut-down the queue, making queue gets and puts raise :exc:`ShutDown`.
243+
244+
By default, gets will only raise once the queue is empty. Set
245+
*immediate* to true to make gets raise immediately instead.
246+
247+
All blocked callers of put() will be unblocked, and also get()
248+
and join() if *immediate* is true.
249+
250+
.. versionadded:: 3.13
251+
252+
217253
SimpleQueue Objects
218254
-------------------
219255

0 commit comments

Comments
 (0)