Naar hoofdinhoud
πŸ”’ Voorbeeldmodus. De eerste vijftien Foundations-lessen zijn gratis; deze is Pro. Start een 7-daagse trial om de editor, AI-hints en de rest van het curriculum te ontgrendelen. Kaart vereist, op elk moment opzegbaar in Dashboard.Start 7-daagse trial β†’
⚑
← Cursussenβ€ΊSenior Deep-DivesModule 1 Β· Gelijktijdigheid en asynchrone interne processenβ€Ίasyncio: diepe duik van gebeurtenisluspredict3 / 161
+100 XP
Opdracht🌐 shown in EN
πŸ“ **Task:** Predict the 5-line interleave from `asyncio.gather(fetch('A', 1), fetch('B', 2))`. Both fetches print 'start NAME' first, then 'done NAME' after their await β€” and gather prints the result list last. πŸ“‹ Implement the function above. Tests run automatically. πŸ’‘ **Hint:** Re-read the theory if you get stuck.
Voorspel uitvoer

Lees de code zorgvuldig

import asyncio

async def fetch(name, delay):
    print(f"start {name}")
    await asyncio.sleep(delay)
    print(f"done {name}")
    return name

async def main():
    results = await asyncio.gather(
        fetch("A", 1),   # sleeps 1 s
        fetch("B", 2),   # sleeps 2 s
    )
    print(results)

asyncio.run(main())

# What does this print? Type your prediction.
# Hint: both fetches enter immediately (gather schedules them
# together). A finishes first (shorter sleep). The list at the end
# matches the ORDER the tasks were passed to gather, NOT the
# completion order.

Wat zal het programma uitprinten? Schrijf hier: