Preskoči na glavni sadržaj
🔒 Način pregleda. Prvih petnaest Foundations lekcija je besplatno; ova je Pro. Pokrenite 7-dnevni trial da otključate editor, AI savjete i ostatak programa. Kartica obavezna, otkažite bilo kada u Dashboard.Pokreni 7-dnevni trial →
⚡
← Kolegiji›Senior Deep-DivesModule 1 · Concurrency internals · Recap›

asyncio.TaskGroup (Python 3.11+) vs gather

predict24 / 161
+125 XP
Zadatak🌐 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.
Predvidi izlaz

Pažljivo pročitaj kod

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

Što će program ispisati? Napiši ovdje: