The following code works fine:
import asyncio loop = asyncio.get_event_loop() async def a (): print('hello') def b (): yield from asyncio.sleep(1) loop.run_until_complete(b()) loop.close() print('done') But, the following fails:
import asyncio loop = asyncio.get_event_loop() async def a (): print('hello') def b (): yield from a() # <=========== only 1 tiny change loop.run_until_complete(b()) loop.close() print('done') Decorating b with @asyncio.coroutine makes it work.
But, the question is why does the first piece of code work fine without the @asyncio.coroutine decorator? The docs clearly say that asyncio.sleep is a coroutine, and so is a, so why does the code fail in one case and work fine for the other case?