GeezerVison is showing github.com — Avoid using iterable coroutines in `asyncio` internally · Issue #100112 · python/cpython
Avoid using iterable coroutines in asyncio internally #100112
Description
asyncio currently uses iterable coroutine directly to wrap awaitables with __await__ methods. This leads to unnecessary special casing and is confusing as asyncio does not supports using yield from now so it should not be used internally too. This will avoid checking for generators everywhere in public APIs (TBD for different issue).
Linked PRs
Activity
kumaraditya303 commented on Dec 9, 2022
ContributorAuthor
More actions
PR #100128 adds a coroutine wrapper around it so that it is not used directly.
kumaraditya303 commented on Mar 3, 2023
ContributorAuthor
More actions
More background on this:
When a custom awaitable object is wrapped by a task, it creates a new iterable coroutine with a generator object as wrapper for the awaitable. This breaks introspection tools which expect a coroutine as they instead get a generator object which is surprising unless you know how things work internally.
import asyncio
import inspect
class Awaitable:
def __await__(self):
return asyncio.sleep(1).__await__()
async def main():
task = asyncio.ensure_future(Awaitable())
coro = task.get_coro()
print(f"Task created with {coro=}")
print(inspect.getcoroutinestate(coro))
asyncio.run(main())
This also somewhat breaks task.get_coro which is expected to return a coroutine object. https://docs.python.org/3/library/asyncio-task.html?highlight=get_coro#asyncio.Task.get_coro
kumaraditya303 commented on Mar 8, 2023
ContributorAuthor
More actions
@gvanrossum #100128 fixes the above and always returns coroutine. The PR has conflicts but this is the idea behind it.
kumaraditya303 commented on Mar 8, 2023
ContributorAuthor
More actions
This will also help in allowing generators to accepted in more places like wait etc but that's for different issue.
Metadata
Metadata
StatusShow more project fields
Development
No branches or pull requestsIssue actions