Prejsť na hlavný obsah
🔒 Režim náhľadu. Prvých pätnásť lekcií Foundations je zadarmo; táto je Pro. Spustite 7-dňový trial pre odomknutie editora, AI nápovied a zvyšku kurzu. Karta vyžadovaná, zrušte kedykoľvek v Dashboard.Spustiť 7-dňový trial →
⚡
← Kurzy›Senior Deep-DivesModule 1 · Concurrency internals · Recap›

asyncio.TaskGroup (Python 3.11+) vs

predict24 / 161
+125 XP
Úloha🌐 shown in EN
📝 **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.
Predpovedz výstup

Prečítaj kód pozorne

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

Čo program vypíše? Napíš sem: