FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-96471: Add queue shutdown, next step. by YvesDup · Pull Request #102499 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
4fd0640
Add threading implementation of queue shutdown
EpicWink Sep 1, 2022
d942c9e
Fix up implementation, add unit-tests
EpicWink Sep 18, 2022
f552ac1
Implement for asyncio queues
EpicWink Sep 18, 2022
78671f9
WIP: multiprocessing queue shutdown
EpicWink Oct 22, 2022
5f31f8e
WIP: multiprocessing queue shutdown
EpicWink Jan 19, 2023
9bbc5db
change comment
YvesDup Feb 10, 2023
f9f2c06
call to self._finished.set() in order to release all joined tasks/coros
YvesDup Feb 10, 2023
7491ef1
add unitests to `shutdwon` method
YvesDup Feb 10, 2023
dd22c6b
add unitests to `shutdwon` method
YvesDup Feb 10, 2023
5239306
replace global state variable with an enum `_QueueState`
YvesDup Feb 10, 2023
4b127b6
replace global state variable with an enum `_QueueState` - erase E ju…
YvesDup Feb 10, 2023
6402de7
simplify and unify tests
YvesDup Feb 11, 2023
be9588b
simplify and unify tests
YvesDup Feb 11, 2023
3613f5d
add `_shutdown_state` to tuples of `__getstate__` and `__setstate__`,…
YvesDup Feb 13, 2023
06775bb
Update initial tests with `self.assertRaises`
YvesDup Feb 15, 2023
6f01015
integration of shudown transition in `shutdown` method
YvesDup Feb 15, 2023
ff9895d
Set `test_shutdown` prefix to all unittests
YvesDup Feb 15, 2023
d42433e
asyncio.queue: refactoring of tests, add new tests, last updates and…
YvesDup Feb 28, 2023
53078bb
first version working
YvesDup Feb 28, 2023
0075039
Some corrections
YvesDup Feb 28, 2023
b4a53d2
Change Enum to global about `shutdown_state` attr
YvesDup Mar 3, 2023
dbe2078
Add new tests
YvesDup Mar 3, 2023
18bb995
Fixes bugs
YvesDup Mar 3, 2023
05700d5
Add first tests
YvesDup Mar 3, 2023
7d01747
Update tests
YvesDup Mar 3, 2023
aad0cba
add _wait()
YvesDup Mar 3, 2023
d9dbb33
Move some tests about shutdwon state and empty queue
YvesDup Mar 3, 2023
9f4c0a3
Merge branch 'python:main' into queue-shutdown
YvesDup Mar 4, 2023
db6a257
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 7, 2023
01e5880
Update 2023-03-07-15-42-27.gh-issue-96471.oWZtwQ.rst
YvesDup Mar 7, 2023
88627bb
Merge branch 'main' into queue-shutdown
YvesDup Mar 7, 2023
37da705
Update test _shutdown_all_methods
YvesDup Mar 7, 2023
795fb2d
Fix some bugs, Refactoring code
YvesDup Mar 8, 2023
0de7836
Merge branch 'main' into queue-shutdown
YvesDup Mar 9, 2023
4443237
Merge branch 'main' into queue-shutdown
YvesDup Mar 15, 2023
499157d
Merge branch 'main' into queue-shutdown
AlexWaygood Mar 15, 2023
99eadd7
Update test names
YvesDup Mar 15, 2023
670d864
Refactoring and fix minor bugs
YvesDup Mar 15, 2023
6af0e8e
suppress import enum
YvesDup Mar 15, 2023
a3e03c5
update docstrings
YvesDup Mar 17, 2023
bc30db7
Suppress `import ctypes`, causes no necessary uses
YvesDup Mar 17, 2023
64defd4
Merge branch 'main' into queue-shutdown
YvesDup Mar 19, 2023
4382409
fix segmentation fault: use ctx.Value,
YvesDup Mar 22, 2023
aa70dc2
fix segmentation fault:
YvesDup Mar 22, 2023
cbfd771
Add private method about shutdown_state
YvesDup Mar 22, 2023
0d095bc
Add private methods to check _shutdown_state attr
YvesDup Mar 24, 2023
6d1f072
Merge branch 'main' into queue-shutdown
YvesDup Mar 24, 2023
891cffe
Update docs for queue shutdown
EpicWink Mar 28, 2023
e30933f
Update Lib/queue.py
YvesDup Mar 29, 2023
de5714d
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
da2e3c7
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
50857c0
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
56272b9
Update Lib/queue.py
YvesDup Mar 29, 2023
db7eaff
Update Lib/queue.py
YvesDup Mar 29, 2023
4099fb8
Update Lib/multiprocessing/queues.py
YvesDup Mar 29, 2023
58007dc
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
225387f
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
7c6e1c7
Update Lib/queue.py
YvesDup Mar 29, 2023
c025766
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
c9ae3be
Update Lib/asyncio/queues.py
YvesDup Mar 29, 2023
e9b66b2
Update some tests
YvesDup Apr 6, 2023
570158e
Suppress a borderline assert
YvesDup Apr 6, 2023
eeb47b0
Fix bugs
YvesDup Apr 6, 2023
6f0b131
Merge branch 'main' into queue-shutdown
YvesDup Apr 7, 2023
4e2a19e
ran patchcheck
YvesDup Apr 7, 2023
6926e11
remove ./Tools/c-analyzer/cpython/_parser.py
YvesDup Apr 7, 2023
d301c90
Merge branch 'main' into queue-shutdown
arhadthedev Apr 10, 2023
49879a0
Merge branch 'main' into queue-shutdown
YvesDup Apr 11, 2023
31ea16b
Add `shutdown` method documentation
YvesDup May 5, 2023
bed3a4b
Add `shutdown` method to documentation
YvesDup May 5, 2023
8e8dcfa
Add `shutdown` method to documentation
YvesDup May 5, 2023
9eed14e
Add `shutdown` method to documentation
YvesDup May 5, 2023
66efd9c
Merge pull request #4 from EpicWink/yvesdup-queue-shutdown-2
YvesDup May 5, 2023
f4ad064
Merge branch 'main' into queue-shutdown
YvesDup May 5, 2023
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Implement for asyncio queues
  • Loading branch information
EpicWink committed Sep 18, 2022
commit f552ac10fa43e5b312092aaeef86076841adf3d1
57 changes: 56 additions & 1 deletion Lib/asyncio/queues.py
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
Expand All @@ -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.

Expand All @@ -41,6 +58,7 @@ def __init__(self, maxsize=0):
self._finished = locks.Event()
self._finished.set()
self._init(maxsize)
self.shutdown_state = _queue_alive

# These three are overridable in subclasses.

Expand Down Expand Up @@ -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:
raise QueueShutDown
while self.full():
putter = self._get_loop().create_future()
self._putters.append(putter)
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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)

class PriorityQueue(Queue):
"""A subclass of Queue; retrieves entries in priority order (lowest first).
Expand Down
57 changes: 57 additions & 0 deletions Lib/test/test_asyncio/test_queues.py
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
Expand Up @@ -522,5 +522,62 @@ class PriorityQueueJoinTests(_QueueJoinTestMixin, unittest.IsolatedAsyncioTestCa
q_class = asyncio.PriorityQueue


class _QueueShutdownTestMixin:
q_class = None

async def test_empty(self):
q = self.q_class()
q.shutdown()
try:
await q.put("data")
self.fail("Didn't appear to shut-down queue")
except asyncio.QueueShutDown:
pass
try:
await q.get()
self.fail("Didn't appear to shut-down queue")
except asyncio.QueueShutDown:
pass

async def test_nonempty(self):
q = self.q_class()
q.put_nowait("data")
q.shutdown()
await q.get()
try:
await q.get()
self.fail("Didn't appear to shut-down queue")
except asyncio.QueueShutDown:
pass

async def test_immediate(self):
q = self.q_class()
q.put_nowait("data")
q.shutdown(immediate=True)
try:
await q.get()
self.fail("Didn't appear to shut-down queue")
except asyncio.QueueShutDown:
pass


class QueueShutdownTests(
_QueueShutdownTestMixin, unittest.IsolatedAsyncioTestCase
):
q_class = asyncio.Queue


class LifoQueueShutdownTests(
_QueueShutdownTestMixin, unittest.IsolatedAsyncioTestCase
):
q_class = asyncio.LifoQueue


class PriorityQueueShutdownTests(
_QueueShutdownTestMixin, unittest.IsolatedAsyncioTestCase
):
q_class = asyncio.PriorityQueue


if __name__ == '__main__':
unittest.main()

Back | FazBrowse Home | New Git URL