Created
January 7, 2022 09:46
-
-
Save narenaryan/0515dd30bb87ba62dcda8319d636030e 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] | |
| # Prints results of co-routines as they are ready | |
| # Beware of Non-deterministic ouput. The order can change based on | |
| # which co-routine finishes first | |
| for cor in asyncio.as_completed(cors): | |
| print(await cor) | |
| asyncio.run(get_results()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment