Async/await vs coroutines from <3.8

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.

  1. 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.
  2. 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

  1. In the new syntax it wants me to use async function and await, but I can’t
    return a value from this?

A plain return yields a: coroutine object.

If you await the coroutine object you get the returned result.

e.g. (minimalistically):

import asyncio
async def f():
    return 42
coro = f()
print(coro)
print(asyncio.run(coro))

Outputs:

<coroutine object f at 0x7fb59bce4048>
42

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())
  1. 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.