Last active
April 12, 2024 17:03
-
-
Save tyler-8/2ea798d031de58e788cccec71f0a9168 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 | |
async def task(seconds: int=5): | |
"""Sleep for some time.""" | |
await asyncio.sleep(seconds) | |
return f"slept {seconds} seconds!" | |
async def multi_task(sleeps_to_sleep: list[int]): | |
"""Start coroutines from a list of inputs.""" | |
all_the_tasks = [task(sleep_time) for sleep_time in sleeps_to_sleep] | |
results = await asyncio.gather(*all_the_tasks) | |
return results | |
def main(): | |
sleeps = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
final_output = asyncio.run(multi_task(sleeps)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment