gh-120924: Simplify consumers by adding asyncio.Queue.iter() and asyncio.Queue.iter_nowait()#120927
Closed
nineteendo wants to merge 42 commits intopython:mainfrom
Closed
gh-120924: Simplify consumers by adding asyncio.Queue.iter() and asyncio.Queue.iter_nowait()#120927nineteendo wants to merge 42 commits intopython:mainfrom
asyncio.Queue.iter() and asyncio.Queue.iter_nowait()#120927nineteendo wants to merge 42 commits intopython:mainfrom
Conversation
Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
…0-30-43.gh-issue-119154.1MHWA-.rst
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Contributor
Author
|
cc @Zac-HD |
Contributor
|
#120491 remains my preferred approach. Given @rhettinger's review of #120503 I still support making More generally, I suggest openning only one or two PRs at a time - having four very similar PRs with multiple issues makes is much harder to track what's going on. |
Contributor
|
Removing review requests on this PR and marking DO NOT MERGE. Let's finish the discussion on #120491 #120491 (comment) |
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.
Purpose
Currently consuming items from a queue is very complex. You need to
queue.task_done()after processing each itemBy adding
asyncio.Queue.iter()andasyncio.Queue.iter_nowait(), this becomes a lot easier, you only need to callqueue.shutdown(). We're using methods to allowqueue.Queueto have the same interface.Overview of changes
queue.Queue.iter()returns an asynchronous generator which iterates over the queue of itemsqueue.Queue.iter_nowait()returns a generator which iterates over the queue of items without blocking._AsyncQueueIteratorhas been added to handle the asynchronous iterationExample
Without iteration
With iteration
Output
*.Queue.iter()and*.Queue.iter_nowait()#120924📚 Documentation preview 📚: https://cpython-previews--120927.org.readthedocs.build/