Skip to content

Instantly share code, notes, and snippets.

@tyler-8
Last active April 12, 2024 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyler-8/2ea798d031de58e788cccec71f0a9168 to your computer and use it in GitHub Desktop.
Save tyler-8/2ea798d031de58e788cccec71f0a9168 to your computer and use it in GitHub Desktop.
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