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 ยท Concurrency internals ยท Recapโ€บAnnuleringspatronen in asynciopredict25 / 161
+125 XP
Opdracht๐ŸŒ 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.
Voorspel uitvoer

Lees de code zorgvuldig

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())

Wat zal het programma uitprinten? Schrijf hier: