summaryrefslogtreecommitdiff
path: root/t/task_groups_and_cancel.py
blob: dcc6bbef8c81cf40563d0d2a17411c0522d82483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import asyncio


async def sleep():
    await asyncio.sleep(3)
    print('I should never finish!')
    return 0


async def work():
    i = 0
    while i < 50:
        i += 1
    await asyncio.sleep(0.2)
    return 0


async def explode():
    await asyncio.sleep(1.5)
    a = 1 / 0
    return a


async def main():
    # exploding will bring all other tasks down with it!
    try:
        async with asyncio.TaskGroup() as tg:
            tg.create_task(sleep())
            tg.create_task(work())
            tg.create_task(explode())
    except:
        pass


asyncio.run(main())