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-DivesModule 1 ยท Concurrency internals ยท Recapโ€บModelli di cancellazione in asinciopredict25 / 161
+125 XP
Compito๐ŸŒ shown in EN
๐Ÿ“ **Question:** Predict the exact output. \`worker\` prints \`start\`, then awaits a long sleep. Main cancels the task. Trace: which prints fire, in what order? ๐Ÿ“‹ Pick the right answer. ๐Ÿ’ก **Hint:** Re-read the theory above if unsure.
Predici output

Leggi il codice attentamente

import asyncio


async def worker():
    try:
        print("start")
        await asyncio.sleep(10)
        print("never")
    except asyncio.CancelledError:
        print("cleanup")
        raise


async def main():
    t = asyncio.create_task(worker())
    await asyncio.sleep(0)
    t.cancel()
    try:
        await t
    except asyncio.CancelledError:
        print("caught")
    print("after")


asyncio.run(main())

Cosa stamperร  il programma? Scrivi qui: