-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-120924: Simplify consumers by adding asyncio.Queue.iter()
and asyncio.Queue.iter_nowait()
#120927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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>
cc @Zac-HD |
#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. |
Removing review requests on this PR and marking DO NOT MERGE. Let's finish the discussion on #120491 #120491 (comment) |
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.Queue
to 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._AsyncQueueIterator
has 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/