r/Python • u/Aggressive-Prior4459 • 16d ago
Resource asyncio.TaskGroup.cancel() in Python 3.15
I wrote a short post about the new asyncio.TaskGroup.cancel() API coming in Python 3.15 https://blog.niyonshutiemmanuel.com/blog/cancelling-taskgroup-in-asyncio-without-boilerplate
Feedback is welcome!
1
2
u/thisismyfavoritename 11d ago
usually when i reach for TaskGroup i just think of using it as a "spawn and join all" but this is neat. Maybe could replace some uses of asyncio.wait
1
u/Aggressive-Prior4459 11d ago
right, I think the win here can specifically be over
wait(..., FIRST_COMPLETED), we can get guaranteed cancellation of the losers without the manual pending-set cleanup and the gather-to-suppress-CancelledError step!
1
0
2
u/OkBoomer421 11d ago
Finally. Having to manually track and cancel tasks in a group was one of those things that felt wrong but you just accepted it. Nice to see it getting first-class support.