Title: Add eager task creation API to asyncio · Issue #97696 · python/cpython · GitHub
Open Graph Title: Add eager task creation API to asyncio · Issue #97696 · python/cpython
X Title: Add eager task creation API to asyncio · Issue #97696 · python/cpython
Description: Feature or enhancement We propose adding “eager” coroutine execution support to asyncio.TaskGroup via a new method enqueue() [1]. TaskGroup.enqueue() would have the same signature as TaskGroup.create_task() but eagerly perform the first ...
Open Graph Description: Feature or enhancement We propose adding “eager” coroutine execution support to asyncio.TaskGroup via a new method enqueue() [1]. TaskGroup.enqueue() would have the same signature as TaskGroup.crea...
X Description: Feature or enhancement We propose adding “eager” coroutine execution support to asyncio.TaskGroup via a new method enqueue() [1]. TaskGroup.enqueue() would have the same signature as TaskGroup.crea...
Opengraph URL: https://github.com/python/cpython/issues/97696
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add eager task creation API to asyncio","articleBody":"# Feature or enhancement\r\n\r\nWe propose adding “eager” coroutine execution support to `asyncio.TaskGroup` via a new method `enqueue()` [1].\r\n\r\n`TaskGroup.enqueue()` would have the same signature as `TaskGroup.create_task()` but eagerly perform the first step of the passed `coroutine`’s execution immediately. If the `coroutine` completes without yielding, the result of `enqueue()` would be an object which behaves like a completed `asyncio.Task`. Otherwise, `enqueue()` behaves the same as `TaskGroup.create_task()`, returning a pending `asyncio.Task`.\r\n\r\nThe reason for a new method, rather than changing the implementation of `TaskGroup.create_task()` is this new method introduces a small semantic difference. For example in:\r\n\r\n```\r\nasync def coro(): ...\r\n\r\nasync with TaskGroup() as tg:\r\n tg.enqueue(coro())\r\n raise Exception\r\n```\r\n\r\nThe exception will cancel everthing scheduled in `tg`, but if some or all of `coro()` completes eagerly any side-effects of this will be observable in further execution. If `tg.create_task()` is used instead no part of `coro()` will be executed.\r\n\r\n\r\n# Pitch\r\n\r\nAt Instagram we’ve observed ~70% of coroutine instances passed to `asyncio.gather()` can run fully synchronously i.e. without performing any I/O which would suspend execution. This typically happens when there is a local cache which can elide actual I/O. We exploit this in [Cinder](https://github.com/facebookincubator/cinder) with a modified `asyncio.gather()` that eagerly executes `coroutine` args and skips scheduling a `asyncio.Task` object to an event loop if no yield occurs. Overall this optimization saved ~4% CPU on our Django webservers. \r\n\r\nIn a prototype implementation of this proposed feature [2] the overhead when scheduling `TaskGroup`s with all fully-synchronous coroutines was decreased by ~8x. When scheduling a mixture of synchronous and asynchronous `coroutine`s, performance is improved by ~1.4x, and when no `coroutine`s can complete synchronously there is still a small improvement.\r\n\r\nWe anticipate code relying on any semantics which change between `TaskGroup.create_task()` and `TaskGroup.enqueue()` will be rare. So, as the TaskGroup interface is new in 3.11, we hope `enqueue()` and its performance benefits can be promoted as the preferred method for scheduling coroutines in 3.12+.\r\n\r\n# Previous discussion\r\n\r\nThis new API was discussed informally at PyCon 2022, with at least some of this being between @gvanrossum, @DinoV, @markshannon, and /or @jbower-fb.\r\n\r\n[1] The name \"`enqueue`\" came out of a discussion between @gvanrossum and @DinoV.\r\n\r\n[2] [Prototype implementation](https://github.com/python/cpython/compare/3.11...DinoV:eagertaskgroup) (some features missing, e.g. specifying Context), and [benchmark](https://gist.github.com/jbower-fb/b52d40bec31d578cadc643b5bad8a485).\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-101613\n* gh-102853\n* gh-104188\n* gh-104189\n* gh-104216\n* gh-104255\n* gh-104256\n* gh-104298\n* gh-104304\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/jbower-fb","@type":"Person","name":"jbower-fb"},"datePublished":"2022-09-30T22:55:46.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":38},"url":"https://github.com/97696/cpython/issues/97696"}
| 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:83cb0718-1c83-4f12-1e3c-6dd7cc655522 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9C4:341673:87FFC5:B6A136:6969BEA4 |
| html-safe-nonce | 97df58834a7394aae7ec60b1ca1b4817fdf90a4c98f810a115b2dfd380e4890f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUM0OjM0MTY3Mzo4N0ZGQzU6QjZBMTM2OjY5NjlCRUE0IiwidmlzaXRvcl9pZCI6Ijg1ODM5MjY1MTAwNDcwNTE0MjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | acd69b3630a21b7232680d2c41a3eb47dabadee93ee58311998f8d3ab7d3df7a |
| hovercard-subject-tag | issue:1393101440 |
| 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/97696/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ba6976ed4883d1fadad2dd7f68b69cd9d006bcb6eb68e1276711a9054f0ec339/python/cpython/issues/97696 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ba6976ed4883d1fadad2dd7f68b69cd9d006bcb6eb68e1276711a9054f0ec339/python/cpython/issues/97696 |
| og:image:alt | Feature or enhancement We propose adding “eager” coroutine execution support to asyncio.TaskGroup via a new method enqueue() [1]. TaskGroup.enqueue() would have the same signature as TaskGroup.crea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jbower-fb |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| 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 | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width