Skip to content

Instantly share code, notes, and snippets.

@bsolomon1124
Last active November 4, 2018 00:01
Show Gist options
  • Select an option

  • Save bsolomon1124/d9320c6b7cf9c8ab0dc3abb23a7541a9 to your computer and use it in GitHub Desktop.

Select an option

Save bsolomon1124/d9320c6b7cf9c8ab0dc3abb23a7541a9 to your computer and use it in GitHub Desktop.
import asyncio
async def foo():
await asyncio.sleep(1)
return 2
async def bar():
f1 = await foo()
f2 = await foo()
return f1, f2
async def foobar():
fb = await asyncio.gather(*(asyncio.ensure_future(f) for f in (foo(), foo()))) # or asyncio.create_task Py 3.7+
return fb
if __name__ == '__main__':
import time
for coro in (foo, bar, foobar):
start = time.monotonic()
res = asyncio.run(coro())
end = time.monotonic()
print(f'{coro.__name__}:\t{end - start}')
# foo: 1.0040620969999998
# bar: 2.0099625779999997
# foobar: 1.0030931179999998
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment