Title: Add `asyncio.Queue.__aiter__` · Issue #119154 · python/cpython · GitHub
Open Graph Title: Add `asyncio.Queue.__aiter__` · Issue #119154 · python/cpython
X Title: Add `asyncio.Queue.__aiter__` · Issue #119154 · python/cpython
Description: Feature or enhancement Proposal: Over the last few years, Trio and AnyIO users have proven out several design patterns using channels as async iterables. For example, having a context manager yield an async iterable avoids the motivating...
Open Graph Description: Feature or enhancement Proposal: Over the last few years, Trio and AnyIO users have proven out several design patterns using channels as async iterables. For example, having a context manager yield...
X Description: Feature or enhancement Proposal: Over the last few years, Trio and AnyIO users have proven out several design patterns using channels as async iterables. For example, having a context manager yield...
Opengraph URL: https://github.com/python/cpython/issues/119154
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `asyncio.Queue.__aiter__`","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\nOver the last few years, Trio and AnyIO users have proven out several design patterns using [channels as async iterables](https://trio.readthedocs.io/en/stable/reference-core.html#using-channels-to-pass-values-between-tasks). For example, having a context manager yield an async iterable avoids the motivating problems of both PEP-533 and PEP-789.\r\n\r\nAn `asyncio.Queue` is almost identical to a channel-pair, especially with the `.shutdown()` method added in Python 3.13. I therefore propose that we add an `.__aiter__` method, to more support such design patterns without subclassing or a generator helper function, with an implementation as described in https://github.com/python/cpython/issues/119154#issuecomment-2120692346\r\n\r\n### Links to previous discussion of this feature:\r\n\r\nhttps://github.com/python/peps/pull/3782#pullrequestreview-2059307313 suggested that `queue.Queue` could also be iterable. If we're extending this to synchronous classes I'd also include `multiprocessing.Queue` and `multiprocessing.SimpleQueue`. I'd omit `multiprocessing.connection.Connection`, due to the byte-level send/recv methods, and `queue.SimpleQueue` because without a `.close()` or `.shutdown()` method there's no clean way to shut down.\r\n\r\n### Limitations\r\n\r\nMaking `Queue` aiterable reaches API parity for single-producer, single-consumer patterns. In multi-producer and/or multi-consumer patterns, without [a `.clone()` method](https://anyio.readthedocs.io/en/stable/streams.html#memory-object-streams) it is the user's responsibility to shut down the queue when the _last_ task is done. I do not propose to add `.clone()`, but we could include that link in the docs as an option for multi-producer patterns if desired.\r\n\r\n\u003c!-- gh-linked-prs -->\r\n### Linked PRs\r\n* gh-120491\r\n* gh-120925\r\n* gh-120503\r\n\u003c!-- /gh-linked-prs -->\r\n\r\n## Decision\r\n\r\nhttps://github.com/python/cpython/pull/120925#issuecomment-2370151879 explains the decision not to add `*.Queue.iter()`, and https://github.com/python/cpython/pull/120491#issuecomment-2372889253 that `asyncio.Queue.__aiter__` was rejected for the same reasons. Instead, the docs will show how users can write a wrapper like this.","author":{"url":"https://github.com/Zac-HD","@type":"Person","name":"Zac-HD"},"datePublished":"2024-05-18T19:57:00Z","url":"https://github.com/python/cpython/issues/119154"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:68fdfdc1-fe79-8266-6500-8548155533b0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DF4:1F95:24294D:32BD7E:6B0241BA |
| html-safe-nonce | ffb88a5e8fb4938f99905de6f169262e8a3b62c3315b61c1580d93a0aa646357 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REY0OjFGOTU6MjQyOTREOjMyQkQ3RTo2QjAyNDFCQSIsInZpc2l0b3JfaWQiOiI1MzMwOTIxNjc3NDAyNTU0ODEwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e556497840b37db1f3a9cc886727857d7cce7861199bbdb584d669700adff6c1 |
| hovercard-subject-tag | issue:2304290807 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/119154/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2c6b218c4599e7edb54a7ba3dc6b31a2d236fb556ce8c38fac3de3f155ea5527/python/cpython/issues/119154 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2c6b218c4599e7edb54a7ba3dc6b31a2d236fb556ce8c38fac3de3f155ea5527/python/cpython/issues/119154 |
| og:image:alt | Feature or enhancement Proposal: Over the last few years, Trio and AnyIO users have proven out several design patterns using channels as async iterables. For example, having a context manager yield... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Zac-HD |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width