Hi.
I am using @asyncio.coroutine with yield and return. and asyncio.gather. It works very well and I can call asyncio.gather from a regular python function. But I see coroutine decorator is deprecated. So I tried the new async and await syntax. It doesn’t work the same. A few observations, and if I’m wrong please correct.
In the new syntax it wants me to use async function and await, but I can’t return a value from this? When I try to use yield (like a proper coroutine should) it turns it into a generator.
This forces all my functions that call functions leading up to an async function to also be async?
I couldn’t get the same behavior as before.
thanks,
Darren
Within an event loop, which asyncio.run() sets up for you, yield works as
you’d expect in Python, returning an async generator rather than a simple
generator:
import asyncio
async def f():
yield 42
await asyncio.sleep(5)
yield 24
async def main():
async for i in f():
print(i)
asyncio.run(main())
This forces all my functions that call functions leading up to an async
function to also be async?
To be running within that thread’s event loop, yes.