Run Multiple Async Tasks Concurrently
Medium
Write an async function fetch_all(delays) where delays is a list of numbers. For each delay, create a coroutine that awaits asyncio.sleep(delay) and then returns that delay value. Use asyncio.gather() to run all coroutines concurrently and return a list of results in the same order as the input.
Input
A list of numeric delays, e.g. [0.1, 0.2, 0.05].
Output
A list of the same numbers, returned after all coroutines complete.
Example
Input
[0.01, 0.02, 0.005]
Output
[0.01, 0.02, 0.005]
Define an inner async function "async def task(delay): await asyncio.sleep(delay); return delay", then use asyncio.gather(*(task(d) for d in delays)).
20 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.