summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2023-05-08 11:59:34 (GMT)
committerGitHub <noreply@github.com>2023-05-08 11:59:34 (GMT)
commitc2683fc46d775d6c4afcb23658c0fd1e328e3c53 (patch)
tree56df722ab88117e5e1d52b7234962e8023941137
parent15665d896bae9c3d8b60bd7210ac1b7dc533b093 (diff)
downloadcpython-c2683fc46d775d6c4afcb23658c0fd1e328e3c53.zip
cpython-c2683fc46d775d6c4afcb23658c0fd1e328e3c53.tar.gz
cpython-c2683fc46d775d6c4afcb23658c0fd1e328e3c53.tar.bz2
gh-97696: Improve and fix documentation for asyncio eager tasks (#104256)
-rw-r--r--Doc/library/asyncio-task.rst19
-rw-r--r--Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst3
2 files changed, 20 insertions, 2 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst
index a46ebc1..b2d7362 100644
--- a/Doc/library/asyncio-task.rst
+++ b/Doc/library/asyncio-task.rst
@@ -560,6 +560,13 @@ Eager Task Factory
using the provided *custom_task_constructor* when creating a new task instead
of the default :class:`Task`.
+ *custom_task_constructor* must be a *callable* with the signature matching
+ the signature of :class:`Task.__init__ <Task>`.
+ The callable must return a :class:`asyncio.Task`-compatible object.
+
+ This function returns a *callable* intended to be used as a task factory of an
+ event loop via :meth:`loop.set_task_factory(factory) <loop.set_task_factory>`).
+
.. versionadded:: 3.12
@@ -1014,7 +1021,7 @@ Introspection
Task Object
===========
-.. class:: Task(coro, *, loop=None, name=None, context=None)
+.. class:: Task(coro, *, loop=None, name=None, context=None, eager_start=False)
A :class:`Future-like <Future>` object that runs a Python
:ref:`coroutine <coroutine>`. Not thread-safe.
@@ -1054,6 +1061,13 @@ Task Object
If no *context* is provided, the Task copies the current context
and later runs its coroutine in the copied context.
+ An optional keyword-only *eager_start* argument allows eagerly starting
+ the execution of the :class:`asyncio.Task` at task creation time.
+ If set to ``True`` and the event loop is running, the task will start
+ executing the coroutine immediately, until the first time the coroutine
+ blocks. If the coroutine returns or raises without blocking, the task
+ will be finished eagerly and will skip scheduling to the event loop.
+
.. versionchanged:: 3.7
Added support for the :mod:`contextvars` module.
@@ -1067,6 +1081,9 @@ Task Object
.. versionchanged:: 3.11
Added the *context* parameter.
+ .. versionchanged:: 3.12
+ Added the *eager_start* parameter.
+
.. method:: done()
Return ``True`` if the Task is *done*.
diff --git a/Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst b/Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst
index 59870de..ced3b7c 100644
--- a/Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst
+++ b/Misc/NEWS.d/next/Library/2023-05-03-16-51-53.gh-issue-104144.653Q0P.rst
@@ -1 +1,2 @@
-Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`. Skip scheduling done callbacks when all tasks finish without blocking.
+Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`.
+Skip scheduling a done callback if a TaskGroup task completes eagerly.