]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102560 Add docstrings to asyncio.TaskGroup (#102565)
authorJosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com>
Wed, 15 Mar 2023 00:33:19 +0000 (00:33 +0000)
committerGitHub <noreply@github.com>
Wed, 15 Mar 2023 00:33:19 +0000 (17:33 -0700)
Lib/asyncio/taskgroups.py

index 911419e1769c17ad53d931bacc4575eefd3660a9..0fdea3697ece3d600ee3c467000ba091e664d971 100644 (file)
@@ -10,7 +10,21 @@ from . import tasks
 
 
 class TaskGroup:
+    """Asynchronous context manager for managing groups of tasks.
 
+    Example use:
+
+        async with asyncio.TaskGroup() as group:
+            task1 = group.create_task(some_coroutine(...))
+            task2 = group.create_task(other_coroutine(...))
+        print("Both tasks have completed now.")
+
+    All tasks are awaited when the context manager exits.
+
+    Any exceptions other than `asyncio.CancelledError` raised within
+    a task will cancel all remaining tasks and wait for them to exit.
+    The exceptions are then combined and raised as an `ExceptionGroup`.
+    """
     def __init__(self):
         self._entered = False
         self._exiting = False
@@ -135,6 +149,10 @@ class TaskGroup:
                 self._errors = None
 
     def create_task(self, coro, *, name=None, context=None):
+        """Create a new task in this group and return it.
+
+        Similar to `asyncio.create_task`.
+        """
         if not self._entered:
             raise RuntimeError(f"TaskGroup {self!r} has not been entered")
         if self._exiting and not self._tasks: