diff options
author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2022-03-14 11:54:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-14 11:54:13 (GMT) |
commit | 9523c0d84f351a610dc651b234461eb015fa3b82 (patch) | |
tree | 5f6f6bed4353eb9c149f65ab2dc95db12d378db3 /Doc | |
parent | 2153daf0a02a598ed5df93f2f224c1ab2a2cca0d (diff) | |
download | cpython-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 'Doc')
-rw-r--r-- | Doc/library/asyncio-eventloop.rst | 11 | ||||
-rw-r--r-- | Doc/library/asyncio-task.rst | 9 |
2 files changed, 17 insertions, 3 deletions
diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 4776853..4f0f8c0 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -330,7 +330,7 @@ Creating Futures and Tasks .. versionadded:: 3.5.2 -.. method:: loop.create_task(coro, *, name=None) +.. method:: loop.create_task(coro, *, name=None, context=None) Schedule the execution of a :ref:`coroutine`. Return a :class:`Task` object. @@ -342,9 +342,16 @@ Creating Futures and Tasks If the *name* argument is provided and not ``None``, it is set as the name of the task using :meth:`Task.set_name`. + An optional keyword-only *context* argument allows specifying a + custom :class:`contextvars.Context` for the *coro* to run in. + The current context copy is created when no *context* is provided. + .. versionchanged:: 3.8 Added the *name* parameter. + .. versionchanged:: 3.11 + Added the *context* parameter. + .. method:: loop.set_task_factory(factory) Set a task factory that will be used by @@ -352,7 +359,7 @@ Creating Futures and Tasks If *factory* is ``None`` the default task factory will be set. Otherwise, *factory* must be a *callable* with the signature matching - ``(loop, coro)``, where *loop* is a reference to the active + ``(loop, coro, context=None)``, where *loop* is a reference to the active event loop, and *coro* is a coroutine object. The callable must return a :class:`asyncio.Future`-compatible object. diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index b30b289..faf5910 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -244,7 +244,7 @@ Running an asyncio Program Creating Tasks ============== -.. function:: create_task(coro, *, name=None) +.. function:: create_task(coro, *, name=None, context=None) Wrap the *coro* :ref:`coroutine <coroutine>` into a :class:`Task` and schedule its execution. Return the Task object. @@ -252,6 +252,10 @@ Creating Tasks If *name* is not ``None``, it is set as the name of the task using :meth:`Task.set_name`. + An optional keyword-only *context* argument allows specifying a + custom :class:`contextvars.Context` for the *coro* to run in. + The current context copy is created when no *context* is provided. + The task is executed in the loop returned by :func:`get_running_loop`, :exc:`RuntimeError` is raised if there is no running loop in current thread. @@ -281,6 +285,9 @@ Creating Tasks .. versionchanged:: 3.8 Added the *name* parameter. + .. versionchanged:: 3.11 + Added the *context* parameter. + Sleeping ======== |