Asyncio Tasks Groups

Description

  • replacement for gather() API

  • Exceptions arre raised as an Exception Group.

Asyncio Tasks Groups example 1

../../_images/tasks_group.png

https://www.slideshare.net/AnthonyShaw5/whats-new-in-python-311

import asynio

async def connect(server: str, port: 1234):
    ...

async def main():
    try:
        async with asyncio.TaskGroup() as tg:
            tg.create_task(connect("localhost", 1234))
            tg.create_task(connect("localhost", 4356))
    except* OSError as eg:
        # handle exception group