Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Last active January 7, 2022 09:34
Show Gist options
  • Save narenaryan/c912a18939d755ef17c6eb30787d3619 to your computer and use it in GitHub Desktop.
Save narenaryan/c912a18939d755ef17c6eb30787d3619 to your computer and use it in GitHub Desktop.
import asyncio
# A co-routine
async def add(x: int, y: int):
return x + y
# Create a function to schedule tasks on the event loop
# then print results
async def get_results():
inputs = [(2,3), (4,5), (5,5), (7,2)]
# Create a task list
tasks = [asyncio.create_task(add(x,y)) for x,y in inputs]
results = asyncio.gather(*tasks)
print(await results) # Prints [5, 9, 10, 9]
asyncio.run(get_results())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment