Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Last active January 6, 2022 19:28
Show Gist options
  • Select an option

  • Save narenaryan/127e184b6567b0ccfeb558ad6cdf0e4d to your computer and use it in GitHub Desktop.

Select an option

Save narenaryan/127e184b6567b0ccfeb558ad6cdf0e4d to your computer and use it in GitHub Desktop.
import asyncio
# A co-routine
async def add(x: int, y: int):
return x + y
# An event loop
loop = asyncio.get_event_loop()
# Create a function to schedule co-routines on the event loop
# then print results and stop the loop
async def get_results():
result1 = await add(3, 4)
result2 = await add(5, 5)
print(result1, result2) # Prints 7 10
loop.stop()
loop.create_task(get_results())
# Blocking call interrupted by loop.stop()
try:
loop.run_forever()
finally:
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment