A class instance as a coroutine #759
Answered
by
smurfix
davidbrochart
asked this question in
Q&A
-
I'd like to have a class instance look like a coroutine: class Foo:
def __await__(self):
return self.run().__await__()
async def run(self):
print("run") But AnyIO doesn't recognize it as a coroutine: import anyio
async def main():
async with anyio.create_task_group() as tg:
tg.start_soon(Foo)
anyio.run(main)
# TypeError: Expected __main__.Foo() to return a coroutine, but the return value (<__main__.Foo object at 0x72557fdc28a0>) is not a coroutine object Is there a better way to write my class so that AnyIO accepts it as a task function? |
Beta Was this translation helpful? Give feedback.
Answered by
smurfix
Jul 18, 2024
Replies: 1 comment 13 replies
-
Not sure if your example has a typo or not, but you're passing the Foo class and not an instance. You have to pass an instance of Foo. |
Beta Was this translation helpful? Give feedback.
13 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry about this; I had patched out that test and my editor refused to restore the original without telling me about it. Sigh.
asyncio is not using
typing.Coroutine
for this test for the simple reason that its code is somewhat older thantyping
.AFAIK unless the asyncio people accept a patch that changes this you're out of luck here.