mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-102560 Add docstrings to asyncio.TaskGroup (#102565)
This commit is contained in:
parent
5fce813d8e
commit
e94edab727
1 changed files with 18 additions and 0 deletions
|
@ -10,7 +10,21 @@ from . import tasks
|
||||||
|
|
||||||
|
|
||||||
class TaskGroup:
|
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):
|
def __init__(self):
|
||||||
self._entered = False
|
self._entered = False
|
||||||
self._exiting = False
|
self._exiting = False
|
||||||
|
@ -135,6 +149,10 @@ class TaskGroup:
|
||||||
self._errors = None
|
self._errors = None
|
||||||
|
|
||||||
def create_task(self, coro, *, name=None, context=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:
|
if not self._entered:
|
||||||
raise RuntimeError(f"TaskGroup {self!r} has not been entered")
|
raise RuntimeError(f"TaskGroup {self!r} has not been entered")
|
||||||
if self._exiting and not self._tasks:
|
if self._exiting and not self._tasks:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue