Last active
January 6, 2022 19:28
-
-
Save narenaryan/127e184b6567b0ccfeb558ad6cdf0e4d 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 | |
| # 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