Created
January 7, 2022 09:23
-
-
Save narenaryan/7a67aaa0044d0bb453a9075c729426b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
# A co-routine | |
async def add(x: int, y: int): | |
return x + y | |
# Create a function to schedule co-routines on the event loop | |
# then print results | |
async def get_results(): | |
inputs = [(2,3), (4,5), (5,5), (7,2)] | |
# Create a co-routine list | |
cors = [add(x,y) for x,y in inputs] | |
results = asyncio.gather(*cors) | |
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