summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2023-05-01 21:10:13 (GMT)
committerGitHub <noreply@github.com>2023-05-01 21:10:13 (GMT)
commita474e04388c2ef6aca75c26cb70a1b6200235feb (patch)
tree43520d5ad16016620f149dc1e84d4d57e45051d5 /Doc
parent59bc36aacddd5a3acd32c80c0dfd0726135a7817 (diff)
downloadcpython-a474e04388c2ef6aca75c26cb70a1b6200235feb.zip
cpython-a474e04388c2ef6aca75c26cb70a1b6200235feb.tar.gz
cpython-a474e04388c2ef6aca75c26cb70a1b6200235feb.tar.bz2
gh-97696: asyncio eager tasks factory (#102853)
Co-authored-by: Jacob Bower <jbower@meta.com> Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/asyncio-task.rst36
-rw-r--r--Doc/whatsnew/3.12.rst5
2 files changed, 41 insertions, 0 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst
index ba0f909..f8727b9 100644
--- a/Doc/library/asyncio-task.rst
+++ b/Doc/library/asyncio-task.rst
@@ -527,6 +527,42 @@ Running Tasks Concurrently
and there is no running event loop.
+Eager Task Factory
+==================
+
+.. function:: eager_task_factory(loop, coro, *, name=None, context=None)
+
+ A task factory for eager task execution.
+
+ When using this factory (via :meth:`loop.set_task_factory(asyncio.eager_task_factory) <loop.set_task_factory>`),
+ coroutines begin execution synchronously during :class:`Task` construction.
+ Tasks are only scheduled on the event loop if they block.
+ This can be a performance improvement as the overhead of loop scheduling
+ is avoided for coroutines that complete synchronously.
+
+ A common example where this is beneficial is coroutines which employ
+ caching or memoization to avoid actual I/O when possible.
+
+ .. note::
+
+ Immediate execution of the coroutine is a semantic change.
+ If the coroutine returns or raises, the task is never scheduled
+ to the event loop. If the coroutine execution blocks, the task is
+ scheduled to the event loop. This change may introduce behavior
+ changes to existing applications. For example,
+ the application's task execution order is likely to change.
+
+ .. versionadded:: 3.12
+
+.. function:: create_eager_task_factory(custom_task_constructor)
+
+ Create an eager task factory, similar to :func:`eager_task_factory`,
+ using the provided *custom_task_constructor* when creating a new task instead
+ of the default :class:`Task`.
+
+ .. versionadded:: 3.12
+
+
Shielding From Cancellation
===========================
diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst
index f4ee30b..a3fce7c 100644
--- a/Doc/whatsnew/3.12.rst
+++ b/Doc/whatsnew/3.12.rst
@@ -613,6 +613,11 @@ Optimizations
* Speed up :class:`asyncio.Task` creation by deferring expensive string formatting.
(Contributed by Itamar O in :gh:`103793`.)
+* Added :func:`asyncio.eager_task_factory` and :func:`asyncio.create_eager_task_factory`
+ functions to allow opting an event loop in to eager task execution,
+ speeding up some use-cases by up to 50%.
+ (Contributed by Jacob Bower & Itamar O in :gh:`102853`)
+
CPython bytecode changes
========================