summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/taskgroups.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2022-02-18 05:30:44 (GMT)
committerGitHub <noreply@github.com>2022-02-18 05:30:44 (GMT)
commitd85121660ea50bbe8fbd31797aa6e4afe0850388 (patch)
tree2c1ed1fe5d1bf4c1d285443320e4e1a2d415a704 /Lib/asyncio/taskgroups.py
parent2a38e1ab65cb28af3babda17f6ad3f8e514a7455 (diff)
downloadcpython-d85121660ea50bbe8fbd31797aa6e4afe0850388.zip
cpython-d85121660ea50bbe8fbd31797aa6e4afe0850388.tar.gz
cpython-d85121660ea50bbe8fbd31797aa6e4afe0850388.tar.bz2
bpo-46752: Slight improvements to TaskGroup API (GH-31398)
* 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. * Pass optional name on to task in create_task() * Remove a bunch of unused stuff
Diffstat (limited to 'Lib/asyncio/taskgroups.py')
-rw-r--r--Lib/asyncio/taskgroups.py22
1 files changed, 4 insertions, 18 deletions
diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py
index 7182778..57b0eaf 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
@@ -15,12 +11,7 @@ from . import tasks
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 +24,8 @@ class TaskGroup:
self._base_error = None
self._on_completed_fut = None
- def get_name(self):
- return self._name
-
def __repr__(self):
- msg = f'<TaskGroup {self._name!r}'
+ msg = f'<TaskGroup'
if self._tasks:
msg += f' tasks:{len(self._tasks)}'
if self._unfinished_tasks:
@@ -152,12 +140,13 @@ class TaskGroup:
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)
@@ -230,6 +219,3 @@ class TaskGroup:
# # after TaskGroup is finished.
self._parent_cancel_requested = True
self._parent_task.cancel()
-
-
-_name_counter = itertools.count(1).__next__