Vai al contenuto principale
๐Ÿ”’ Modalitร  anteprima. Le prime quindici lezioni di Foundations sono gratuite; questa รจ Pro. Avvia un trial di 7 giorni per sbloccare l'editor, i suggerimenti AI e il resto del programma. Carta richiesta, disdici in qualsiasi momento dalla Dashboard.Avvia trial di 7 giorni โ†’
โšก
โ† Corsiโ€บSenior Deep-DivesModulo 1 ยท Concorrenza e aspetti interni asincroniโ€บasyncio: approfondimento del ciclo di eventipredict3 / 161
+100 XP
Compito๐ŸŒ 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.
Predici output

Leggi il codice attentamente

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.

Cosa stamperร  il programma? Scrivi qui: