Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Created January 6, 2022 18:39
Show Gist options
  • Select an option

  • Save narenaryan/576e62dfb5cbe611b27688b22438a749 to your computer and use it in GitHub Desktop.

Select an option

Save narenaryan/576e62dfb5cbe611b27688b22438a749 to your computer and use it in GitHub Desktop.
import asyncio
# A fast co-routine
async def add_fast(x, y):
print("starting fast add")
await asyncio.sleep(3) # Mimic some network delay
print("result ready for fast add")
return x + y
# A slow co-routine
async def add_slow(x, y):
print("starting slow add")
await asyncio.sleep(5) # # Mimic some network delay
print("result ready for slow add")
return x + y
# Create a function to schedule co-routines
async def get_results():
task1 = asyncio.create_task(add_slow(3, 4))
task2 = asyncio.create_task(add_fast(5, 5))
print(await task1, await task2) # Prints 7 10
asyncio.run(get_results())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment