4 ms·
About this new asyncio.TaskGroup thing, I found this from Guido on the related GH issue* > After some conversations with Yury, and encouraged by the SC's appro
by nicoco 4y ago
About this new asyncio.TaskGroup thing, I found this from Guido on the related GH issue*
> After some conversations with Yury, and encouraged by the SC's approval of PEP-654, I am proposing to add a new class, asyncio.TaskGroup, which introduces structured concurrency similar to nurseries in Trio.
I have never used but have been told that Trio's nurseries make it much easier to handle exceptions in asyncio tasks. Does someone more knowledgeable can tell if this will help? Looking at the docs*, this only seems to be a helper when you want to await several tasks at once, so I am not sure this changes much for exception handling.
* https://github.com/python/cpython/issues/90908 https://github.com/python/cpython/issues/90908
** https://docs.python.org/3.11/library/asyncio-task.html#task-groups https://docs.python.org/3.11/library/asyncio-task.html#task-...
- d0mine 4y agoThe context here is that TaskGroup + except* enable "structured concurrency" https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ https://vorpus.org/blog/notes-on-structured-concurrency-or-g...
- quietbritishjim 4y agoAs an emperical point, I moved from asyncio to Trio and it was transformative. This will help bring asyncio almost up to parity but it's a pity that it's still possible to make tasks that don't belong to a task group - in Trio, the only way to start a task is to run it in a specified nursery. (But of course understandable for backwards compatibility.) > this only seems to be a helper when you want to await several tasks at once Sort of. It's a helper for if you want to run multiple tasks at once, not necessarily awaiting them. And you're definitely running multiple tasks at once otherwise you wouldn't be using asyncio in the first place. Task groups do require you to wait for the tasks - after all, you have to start the task in a task group, and then implicit await the tasks in it (by falling off the end of the task group context block). But you can always have an outer task group representing tasks that you indent to run indefinitely in the background. In that way, task groups force you to think about when a task would cancel other tasks, representing the overall structure of your program.
- linkdd 4y agoTrio is truly great, and they implemented MultiErrors (ExceptionGroup) long before it was considered to be a PEP. I managed to make a very very simple OTP-like framework with Trio: https://linkdd.github.io/triotp/ https://linkdd.github.io/triotp/
- pavon 4y agoNice, Python 3.11: Python for TaskGroups