Skip to content

Commit

Permalink
gh-102560 Add docstrings to asyncio.TaskGroup (#102565)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSBoyle authored Mar 15, 2023
1 parent 5fce813 commit e94edab
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@


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
Expand Down Expand Up @@ -135,6 +149,10 @@ async def __aexit__(self, et, exc, tb):
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:
Expand Down

0 comments on commit e94edab

Please sign in to comment.