TaskGroup and create_task method.
Hello all, I am trying the TaskGroup class in the python 3.11 beta version. And I like it a lot. It makes asynchronous programming intuitive enough even for the less experienced like myself. I can see myself teaching this to my colleges when they run linux parallel subprocesses. I will try to convince our IT department to download 3.11 when it’s officially out mostly because of new improved asyncio support. The speed gain will be another good justification. However, excuse my ignorance, I have a question... Why can’t I use TaskGroup.create_task method (or asyncio.create_task function) with a Future object (eg. output of loop.run_in_executor function) since it’s an awaitable (i.e., I can do “await future”)? In other words, why can’t create_task accept all awaitable objects? I can wrap the loop.run_in_executor in an async function and call that function inside create_task with no issues. It would be nice to do the following: async def func(num: int) -> int: await asyncio.sleep(5) return num def blocking_function(*args: float) -> float: time.sleep(5) sum(args) async def func() -> tuple[int, float]: loop = asyncio.get_event_loop() args = (1, 2, 3) async with asyncio.TaskGroup() as tg: task1 = tg.create_task(func(5)) task2 = tg.create_task(loop.run_in_executor(None, blocking_function, *args)) results = await task1, await task2 return results Also, is there a way I can put a timeout for the task group so I can handle it using except* TimeOutError? Also, if I handled/extracted the exception, can I uncancel some of the tasks (that were canceled due to the exception handling) that already took long time so I can resume it in another GroupTask? Thanks, Abdulla
participants (1)
-
Abdulla Al Kathiri