summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/taskgroups.py
diff options
context:
space:
mode:
authorAndrew Svetlov <andrew.svetlov@gmail.com>2022-03-14 11:54:13 (GMT)
committerGitHub <noreply@github.com>2022-03-14 11:54:13 (GMT)
commit9523c0d84f351a610dc651b234461eb015fa3b82 (patch)
tree5f6f6bed4353eb9c149f65ab2dc95db12d378db3 /Lib/asyncio/taskgroups.py
parent2153daf0a02a598ed5df93f2f224c1ab2a2cca0d (diff)
downloadcpython-9523c0d84f351a610dc651b234461eb015fa3b82.zip
cpython-9523c0d84f351a610dc651b234461eb015fa3b82.tar.gz
cpython-9523c0d84f351a610dc651b234461eb015fa3b82.tar.bz2
bpo-46994: Accept explicit contextvars.Context in asyncio create_task() API (GH-31837)
Diffstat (limited to 'Lib/asyncio/taskgroups.py')
-rw-r--r--Lib/asyncio/taskgroups.py7
1 files changed, 5 insertions, 2 deletions
diff --git a/Lib/asyncio/taskgroups.py b/Lib/asyncio/taskgroups.py
index c3ce94a..6af21f3 100644
--- a/Lib/asyncio/taskgroups.py
+++ b/Lib/asyncio/taskgroups.py
@@ -138,12 +138,15 @@ class TaskGroup:
me = BaseExceptionGroup('unhandled errors in a TaskGroup', errors)
raise me from None
- def create_task(self, coro, *, name=None):
+ def create_task(self, coro, *, name=None, context=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)
+ if context is None:
+ task = self._loop.create_task(coro)
+ else:
+ task = self._loop.create_task(coro, context=context)
tasks._set_task_name(task, name)
task.add_done_callback(self._on_task_done)
self._unfinished_tasks += 1