Skip to content

Commit c2683fc

Browse files
authored
gh-97696: Improve and fix documentation for asyncio eager tasks (#104256)
1 parent 15665d8 commit c2683fc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Doc/library/asyncio-task.rst

+18-1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ Eager Task Factory
560560
using the provided *custom_task_constructor* when creating a new task instead
561561
of the default :class:`Task`.
562562

563+
*custom_task_constructor* must be a *callable* with the signature matching
564+
the signature of :class:`Task.__init__ <Task>`.
565+
The callable must return a :class:`asyncio.Task`-compatible object.
566+
567+
This function returns a *callable* intended to be used as a task factory of an
568+
event loop via :meth:`loop.set_task_factory(factory) <loop.set_task_factory>`).
569+
563570
.. versionadded:: 3.12
564571

565572

@@ -1014,7 +1021,7 @@ Introspection
10141021
Task Object
10151022
===========
10161023

1017-
.. class:: Task(coro, *, loop=None, name=None, context=None)
1024+
.. class:: Task(coro, *, loop=None, name=None, context=None, eager_start=False)
10181025

10191026
A :class:`Future-like <Future>` object that runs a Python
10201027
:ref:`coroutine <coroutine>`. Not thread-safe.
@@ -1054,6 +1061,13 @@ Task Object
10541061
If no *context* is provided, the Task copies the current context
10551062
and later runs its coroutine in the copied context.
10561063

1064+
An optional keyword-only *eager_start* argument allows eagerly starting
1065+
the execution of the :class:`asyncio.Task` at task creation time.
1066+
If set to ``True`` and the event loop is running, the task will start
1067+
executing the coroutine immediately, until the first time the coroutine
1068+
blocks. If the coroutine returns or raises without blocking, the task
1069+
will be finished eagerly and will skip scheduling to the event loop.
1070+
10571071
.. versionchanged:: 3.7
10581072
Added support for the :mod:`contextvars` module.
10591073

@@ -1067,6 +1081,9 @@ Task Object
10671081
.. versionchanged:: 3.11
10681082
Added the *context* parameter.
10691083

1084+
.. versionchanged:: 3.12
1085+
Added the *eager_start* parameter.
1086+
10701087
.. method:: done()
10711088

10721089
Return ``True`` if the Task is *done*.
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`. Skip scheduling done callbacks when all tasks finish without blocking.
1+
Optimize :class:`asyncio.TaskGroup` when using :func:`asyncio.eager_task_factory`.
2+
Skip scheduling a done callback if a TaskGroup task completes eagerly.

0 commit comments

Comments
 (0)