This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Awaiting an awaitable returned from an async function does nothing
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: allenap, gvanrossum, yselivanov
Priority: normal Keywords:

Created on 2016-11-17 15:43 by allenap, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg281043 - (view) Author: Gavin Panella (allenap) Date: 2016-11-17 15:43
The following will sleep:

  async def one():
      await asyncio.sleep(10)

  async def two():
      await one()

  loop.run_until_complete(two())

but the following will not:

  async def one():
      return asyncio.sleep(10)

  async def two():
      await one()

  loop.run_until_complete(two())

I would expect run_until_complete to keep bouncing awaitable results back into the event-loop until a non-awaitable is returned. In my code I work around this with:

  result = loop.run_until_complete(...)
  while inspect.isawaitable(result):
      result = loop.run_until_complete(result)

I would also expect that the await in `two` would have DTRT with the returned generator/coroutine.
msg281055 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-11-17 17:02
This isn't a bug.  Python doesn't magically unwind awaitables, you have to do that yourself:

  async def two():
      await (await one())

Broadly speaking, returning awaitables from coroutines is an anti-pattern.

Closing this one. Feel free to re-open or ask questions :)
History
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72911
2016-11-17 17:02:49yselivanovsetstatus: open -> closed
resolution: not a bug
messages: + msg281055

stage: resolved
2016-11-17 15:43:29allenapcreate