summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/asyncio_cancel_fair.py
blob: a1e35f82ba6e0d442ebf218bc8d1d4d45b8a5907 (plain) (blame)
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
# Test fairness of cancelling a task
# That tasks which continuously cancel each other don't take over the scheduler

try:
    import asyncio
except ImportError:
    print("SKIP")
    raise SystemExit


async def task(id, other):
    for i in range(3):
        try:
            print("start", id)
            await asyncio.sleep(0)
            print("done", id)
        except asyncio.CancelledError as er:
            print("cancelled", id)
        if other is not None:
            print(id, "cancels", other)
            tasks[other].cancel()


async def main():
    global tasks
    tasks = [
        asyncio.create_task(task(0, 1)),
        asyncio.create_task(task(1, 0)),
        asyncio.create_task(task(2, None)),
    ]
    await tasks[2]


asyncio.run(main())