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 1 · Concurrency internals · RecapCancellation patterns in asynciopredict25 / 161
+125 XP
Task
📝 **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.
Predict the output

Read the code carefully

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

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…