Repository navigation
Conversation
+ add 3 private attributes, + add new "QueueWithPendingTasks" exception, + update put_nowait, getnowait methods, could raise this new exception, + update _wakeup_next method, + update __repr__, + add 2 unit tests,
get/put item in asyncio.Queue when there are pending task or woken tasks
get/put item in asyncio.Queue when there are pending task or woken tasksget/put item in asyncio.Queue when there are pending task or woken tasks
Contributor
Author
|
@asvetlov : A draft PR with an another proposal. This PR also fixes this issue for the following example: async def simplier_get_case():
async def get_and_print(q):
item = await q.get()
print(f"\tGET: {item}, {str(q) = }")
q = asyncio.Queue(1)
g = 3
for n in range(g):
asyncio.create_task(get_and_print(q))
await asyncio.sleep(0.0)
# queue full: 1 item in queue, 2 items waiting into the _putters
n = 1
print(f'PUT: {await q.put(n) or n}')
n += 1
# queue not empty: 1 item in queue, 2 tasks the _putters
# steal the free solt
try:
print(f"\tGET_NOWAIT steal a free slot: {q.get_nowait()}")
except:
g -= 1
print("Failed to GET_NOWAIT item".center(60, '-'))
# get all items
for _ in range(g):
print(f'PUT: {await q.put(n) or n}')
n += 1
await asyncio.sleep(0.0)
asyncio.run(simplier_get_case()) |
get/put item in asyncio.Queue when there are pending task or woken tasksget/put[_nowait] item in asyncio.Queue when there woken tasks
get/put[_nowait] item in asyncio.Queue when there woken tasksget/put[_nowait] item in asyncio.Queue when there are woken tasks
+ in Queue class - remove ``QueueWithpendingTasks`` exception and replace with existing ``QueueFull`` and ``QueueEmpty`` with a specific message. - simplify new tests that raise these exceptions. - fix update to woken counters. + rewrite tests.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The proposal fixes 2 main problems when tasks are woken just after the retrieval of an item, or after its insertion when queue is empty
Just after the retrieval of an item, a new slot is free and a pending putter is awaken (case without pending putters is not concern). So this free slot that can be stole by
put[_nowait]statement. Moreover this issue can break the FIFO's order of the queue.In this case, a
QueueFullexception is raised with an explicit messageJust after the insertion of an item in an empty queue with pending getters, one of the pending getter is awaken to get this fresh item at the next iteration. But this item can be get via a
get[_nowait]statement before the awaken task.In this case, a
QueueEmptyexception is raised with an explicit message.