Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 8 · Async correctness patternsasyncio.as_completed — stream results as tasks finishpredict128 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. Three tasks named slow / fast / medium are scheduled with respective delays 30ms / 10ms / 20ms. They're submitted in the order [slow, fast, medium], but as_completed yields them as they finish. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

import asyncio


async def task(name: str, delay: float) -> str:
    await asyncio.sleep(delay)
    return name


async def main() -> None:
    tasks = [task("slow", 0.03), task("fast", 0.01), task("medium", 0.02)]
    order: list[str] = []
    for coro in asyncio.as_completed(tasks):
        result = await coro
        order.append(result)
    print(order)


asyncio.run(main())

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…