How to cancel all tasks created within same TaskGroup

How about:

import asyncio
import random

async def coro(id, tasks):
    while True:
        await asyncio.sleep(0)
        if random.random() < 0.3:
            print(f"coro {id} is cancelling all coros")
            for task in tasks:
                task.cancel()
            break
        print(f"coro {id} is alive")

async def main():
    tasks = []
    async with asyncio.TaskGroup() as tg:
        tasks.append(tg.create_task(coro(0, tasks)))
        tasks.append(tg.create_task(coro(1, tasks)))

asyncio.run(main())