gh-83055: asyncio.Queue: putting items out of order when it is full #112501
+406
−10
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.


Comment
Currently, when the queue is full, new items are added to the
queue._puttersdeque via an attached future. As soon as an item is removed from this queue, one future of the deque is set todone, then removed. This future is in transit, not referenced anywhere.First I suggest removing the future from
queue._puttersonly after it had been woken up, not before. All pending or moving item/task will stay intoqueue._puttersSecond when
put()is called, if the queue is full or ifqueue._puttersis not empty, this item/task will be added toqueue._putters.When
put_nowait()is called, if the queue is not full andqueue._puttersis not empty, we raise a newQueueFullWithPendingPutTasksexception derived fromQueueFull.The behavior of the
getpart should be identical.Modified methods of
Queueclass :put()put_nowait()get()get_nowait()_wakeup_next()New private methods of
Queueclass:_put_and_wakeup_next()called fromput()andput_nowait()._get_and_wakeup_next()called fromget()andget_nowait().New exceptions of
queues.py:QueueFullWithPendingPutTasksraised fromput_nowait()QueueEmptyWithPendingGetTasksraised fromget_nowait()Update:
These two exceptions are derived from
QueueFullandQueueEmpyrespectively. This is necessary to preserve thebackwards compatibility of
put_nowait()andget_nowait()methods.May be their names are too long or not explicit enough, so I am open to suggestions.