Created
January 6, 2022 18:39
-
-
Save narenaryan/576e62dfb5cbe611b27688b22438a749 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 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