From 32468ef98e0e8c9e83fe9d738571b886774d5c43 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 16 Feb 2022 18:24:53 -0800 Subject: [PATCH 1/3] Remove task group names (for now) We're not sure that they are needed, and once in the code we would never be able to get rid of them. Yury wrote: > Ideally, there should be a way for someone to build a "trace" > of taskgroups/task leading to the current running task. > We could do that using contextvars, but I'm not sure we should > do that in 3.11. --- Lib/asyncio/taskgroups.py | 12 ++---------- Lib/test/test_asyncio/test_taskgroups.py | 12 ++++++------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 718277892c51c9..39d261dafac826 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -15,12 +15,7 @@ class TaskGroup: - def __init__(self, *, name=None): - if name is None: - self._name = f'tg-{_name_counter()}' - else: - self._name = str(name) - + def __init__(self): self._entered = False self._exiting = False self._aborting = False @@ -33,11 +28,8 @@ def __init__(self, *, name=None): self._base_error = None self._on_completed_fut = None - def get_name(self): - return self._name - def __repr__(self): - msg = f' Date: Wed, 16 Feb 2022 18:29:28 -0800 Subject: [PATCH 2/3] Pass name on to task in create_task() --- Lib/asyncio/taskgroups.py | 3 ++- Lib/test/test_asyncio/test_taskgroups.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 39d261dafac826..83db4682c755ca 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -144,12 +144,13 @@ async def __aexit__(self, et, exc, tb): me = BaseExceptionGroup('unhandled errors in a TaskGroup', errors) raise me from None - def create_task(self, coro): + def create_task(self, coro, *, name=None): if not self._entered: raise RuntimeError(f"TaskGroup {self!r} has not been entered") if self._exiting and self._unfinished_tasks == 0: raise RuntimeError(f"TaskGroup {self!r} is finished") task = self._loop.create_task(coro) + tasks._set_task_name(task, name) task.add_done_callback(self._on_task_done) self._unfinished_tasks += 1 self._tasks.add(task) diff --git a/Lib/test/test_asyncio/test_taskgroups.py b/Lib/test/test_asyncio/test_taskgroups.py index 8895fd57674223..aab1fd1ebb38d8 100644 --- a/Lib/test/test_asyncio/test_taskgroups.py +++ b/Lib/test/test_asyncio/test_taskgroups.py @@ -692,3 +692,10 @@ async def runner(): self.assertEqual(get_error_types(cm.exception), {ZeroDivisionError}) self.assertGreaterEqual(nhydras, 10) + + async def test_taskgroup_task_name(self): + async def coro(): + await asyncio.sleep(0) + async with taskgroups.TaskGroup() as g: + t = g.create_task(coro(), name="yolo") + self.assertEqual(t.get_name(), "yolo") From 220bb6d1f5aa83e0df5f46e4b09707a52d67f5c2 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 16 Feb 2022 21:47:30 -0800 Subject: [PATCH 3/3] Remove a bunch of unused stuff --- Lib/asyncio/taskgroups.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py index 83db4682c755ca..57b0eafefc16fe 100644 --- a/Lib/asyncio/taskgroups.py +++ b/Lib/asyncio/taskgroups.py @@ -3,10 +3,6 @@ __all__ = ["TaskGroup"] -import itertools -import textwrap -import traceback -import types import weakref from . import events @@ -223,6 +219,3 @@ def _on_task_done(self, task): # # after TaskGroup is finished. self._parent_cancel_requested = True self._parent_task.cancel() - - -_name_counter = itertools.count(1).__next__