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 β†’
⚑
← Coursesβ€ΊSenior Deep-DivesModule 1 Β· Concurrency internals Β· Recapβ€Ίasyncio.TaskGroup (Python 3.11+) vs gatherpredict24 / 161
+125 XP
Task
πŸ“ **Question:** Predict the exact output. Three tasks start together: A finishes at t=0.01, B raises ValueError at t=0.05, C would finish at t=0.10. What happens to C when B raises? How many exceptions land in the \`except*\` group? πŸ“‹ Pick the right answer. πŸ’‘ **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

import asyncio


async def task(name, delay, fail=False):
    await asyncio.sleep(delay)
    if fail:
        print(f"raise:{name}")
        raise ValueError(name)
    print(f"done:{name}")


async def main():
    try:
        async with asyncio.TaskGroup() as tg:
            tg.create_task(task("A", 0.01))
            tg.create_task(task("B", 0.05, fail=True))
            tg.create_task(task("C", 0.10))
    except* ValueError as eg:
        print(f"caught:{len(eg.exceptions)}")
    print("after")


asyncio.run(main())

What will the program print? Write here: