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 asyncio.QueueWithPendingTasks:
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()) |
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 here needs a new exception that would be replaced with
QueueEmpty/QueueFullwith a specific messageQueueWithPendingTasksexception,put_nowait,get_nowaitmethods, could raise this new exception,_wakeup_nextmethod,__repr__,