-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-96471: Add queue shutdown #96474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
gh-96471: Add queue shutdown #96474
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4fd0640
Add threading implementation of queue shutdown
EpicWink d942c9e
Fix up implementation, add unit-tests
EpicWink f552ac1
Implement for asyncio queues
EpicWink 78671f9
WIP: multiprocessing queue shutdown
EpicWink 5f31f8e
WIP: multiprocessing queue shutdown
EpicWink a2035a6
Merge branch 'main' into queue-shutdown
arhadthedev 978b8d1
Replace try-catch with assert-raises ctx-mgrs
EpicWink b19f529
WIP: multiprocessing queue shut-down method
EpicWink 6d3b3a5
Use enum for asyncio queue shutdown state
EpicWink 85827f1
Make asyncio queue state private
EpicWink 121de12
Replace try-catch with assert-raises ctx-mgrs
EpicWink 5e2df02
Add asyncio queue tests
EpicWink 14c53d7
Release joined awaitables
EpicWink da10d9f
Zero unfinished tasks
EpicWink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,11 @@ | ||||||
__all__ = ('Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty') | ||||||
__all__ = ( | ||||||
'Queue', | ||||||
'PriorityQueue', | ||||||
'LifoQueue', | ||||||
'QueueFull', | ||||||
'QueueEmpty', | ||||||
'QueueShutDown', | ||||||
) | ||||||
|
||||||
import collections | ||||||
import heapq | ||||||
|
@@ -18,6 +25,16 @@ class QueueFull(Exception): | |||||
pass | ||||||
|
||||||
|
||||||
class QueueShutDown(Exception): | ||||||
"""Raised when putting on to or getting from a shut-down Queue.""" | ||||||
pass | ||||||
|
||||||
|
||||||
_queue_alive = "alive" | ||||||
_queue_shutdown = "shutdown" | ||||||
_queue_shutdown_immediate = "shutdown-immediate" | ||||||
|
||||||
|
||||||
class Queue(mixins._LoopBoundMixin): | ||||||
"""A queue, useful for coordinating producer and consumer coroutines. | ||||||
|
@@ -41,6 +58,7 @@ def __init__(self, maxsize=0): | |||||
self._finished = locks.Event() | ||||||
self._finished.set() | ||||||
self._init(maxsize) | ||||||
self.shutdown_state = _queue_alive | ||||||
EpicWink marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
# These three are overridable in subclasses. | ||||||
|
||||||
|
@@ -113,6 +131,8 @@ async def put(self, item): | |||||
Put an item into the queue. If the queue is full, wait until a free | ||||||
slot is available before adding item. | ||||||
""" | ||||||
if self.shutdown_state != _queue_alive: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an example:
Suggested change
|
||||||
raise QueueShutDown | ||||||
while self.full(): | ||||||
putter = self._get_loop().create_future() | ||||||
self._putters.append(putter) | ||||||
|
@@ -132,13 +152,17 @@ async def put(self, item): | |||||
# the call. Wake up the next in line. | ||||||
self._wakeup_next(self._putters) | ||||||
raise | ||||||
if self.shutdown_state != _queue_alive: | ||||||
raise QueueShutDown | ||||||
return self.put_nowait(item) | ||||||
|
||||||
def put_nowait(self, item): | ||||||
"""Put an item into the queue without blocking. | ||||||
If no free slot is immediately available, raise QueueFull. | ||||||
""" | ||||||
if self.shutdown_state != _queue_alive: | ||||||
raise QueueShutDown | ||||||
if self.full(): | ||||||
raise QueueFull | ||||||
self._put(item) | ||||||
|
@@ -151,7 +175,11 @@ async def get(self): | |||||
If queue is empty, wait until an item is available. | ||||||
""" | ||||||
if self.shutdown_state == _queue_shutdown_immediate: | ||||||
raise QueueShutDown | ||||||
while self.empty(): | ||||||
if self.shutdown_state != _queue_alive: | ||||||
raise QueueShutDown | ||||||
getter = self._get_loop().create_future() | ||||||
self._getters.append(getter) | ||||||
try: | ||||||
|
@@ -170,6 +198,8 @@ async def get(self): | |||||
# the call. Wake up the next in line. | ||||||
self._wakeup_next(self._getters) | ||||||
raise | ||||||
if self.shutdown_state == _queue_shutdown_immediate: | ||||||
raise QueueShutDown | ||||||
return self.get_nowait() | ||||||
|
||||||
def get_nowait(self): | ||||||
|
@@ -178,7 +208,11 @@ def get_nowait(self): | |||||
Return an item if one is immediately available, else raise QueueEmpty. | ||||||
""" | ||||||
if self.empty(): | ||||||
if self.shutdown_state != _queue_alive: | ||||||
raise QueueShutDown | ||||||
raise QueueEmpty | ||||||
elif self.shutdown_state == _queue_shutdown_immediate: | ||||||
raise QueueShutDown | ||||||
item = self._get() | ||||||
self._wakeup_next(self._putters) | ||||||
return item | ||||||
|
@@ -214,6 +248,27 @@ async def join(self): | |||||
if self._unfinished_tasks > 0: | ||||||
await self._finished.wait() | ||||||
|
||||||
def shutdown(self, immediate=False): | ||||||
"""Shut-down the queue, making queue gets and puts raise. | ||||||
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 put() will be unblocked, and also get() | ||||||
and join() if 'immediate'. The QueueShutDown exception is raised. | ||||||
""" | ||||||
if immediate: | ||||||
self.shutdown_state = _queue_shutdown_immediate | ||||||
while self._getters: | ||||||
getter = self._getters.popleft() | ||||||
if not getter.done(): | ||||||
getter.set_result(None) | ||||||
else: | ||||||
self.shutdown_state = _queue_shutdown | ||||||
while self._putters: | ||||||
putter = self._putters.popleft() | ||||||
if not putter.done(): | ||||||
putter.set_result(None) | ||||||
EpicWink marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
class PriorityQueue(Queue): | ||||||
"""A subclass of Queue; retrieves entries in priority order (lowest first). | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.