Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chaudum
Created June 18, 2019 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaudum/d2bdb2ae27847dfdf5f19bab1ca109c8 to your computer and use it in GitHub Desktop.
Save chaudum/d2bdb2ae27847dfdf5f19bab1ca109c8 to your computer and use it in GitHub Desktop.
import asyncio
async def do_something_long_running(sec=1) -> bool:
print(f"sleep for {sec}s")
await asyncio.sleep(sec)
return True
async def coro_a(t):
return await do_something_long_running(t)
async def coro_b(t):
return await not_a_coro(t)
async def coro_c(t):
return not_a_coro(t)
def not_a_coro(t):
return do_something_long_running(t)
async def main():
print(await coro_a(1))
print(await coro_b(2))
print(await coro_c(3))
if __name__ == "__main__":
asyncio.run(main())
@chaudum
Copy link
Author

chaudum commented Jun 18, 2019

$ python3.7 asyncio_return_values.py
sleep for 1s
True
sleep for 2s
True
<coroutine object do_something_long_running at 0x7f94107511c8>
asyncio_return_values.py:29: RuntimeWarning: coroutine 'do_something_long_running' was never awaited
  print(await coro_c(3))
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment