Last active
November 23, 2021 20:11
-
-
Save nmchenry01/7f02fbf5c9099c6c05a38963dfc8171d to your computer and use it in GitHub Desktop.
Running multiple coroutines sequentially
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 | |
| import time | |
| async def me_first(): | |
| await asyncio.sleep(1) | |
| return "I'm first!" | |
| async def me_second(): | |
| await asyncio.sleep(1) | |
| return "Then me!" | |
| async def me_third(): | |
| await asyncio.sleep(1) | |
| return "And finally me!" | |
| async def main(): | |
| start = time.perf_counter() | |
| first_result = await me_first() | |
| second_result = await me_second() | |
| third_result = await me_third() | |
| end = time.perf_counter() | |
| print(f"It took {end - start} seconds to complete all operations\n") | |
| print(first_result) | |
| print(second_result) | |
| print(third_result) | |
| asyncio.run(main()) | |
| # Output: | |
| # | |
| # It took 3.0030471999998554 seconds to complete all operations | |
| # | |
| # I'm first! | |
| # Then me! | |
| # And finally me! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment