Skip to content

Commit 2ba0d19

Browse files
committed
update docs about create_task
1 parent 2fd6255 commit 2ba0d19

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

Doc/library/asyncio-eventloop.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ Creating Futures and Tasks
355355

356356
.. versionadded:: 3.5.2
357357

358-
.. method:: loop.create_task(coro, *, name=None, context=None)
358+
.. method:: loop.create_task(coro, *, name=None, context=None, eager_start=None, **kwargs)
359359

360360
Schedule the execution of :ref:`coroutine <coroutine>` *coro*.
361361
Return a :class:`Task` object.
@@ -371,12 +371,27 @@ Creating Futures and Tasks
371371
custom :class:`contextvars.Context` for the *coro* to run in.
372372
The current context copy is created when no *context* is provided.
373373

374+
An optional keyword-only *eager_start* argument allows eagerly starting
375+
the execution of the :class:`asyncio.Task` at task creation time.
376+
If set to ``True`` and the event loop is running, the task will start
377+
executing the coroutine immediately, until the first time the coroutine
378+
blocks. If the coroutine returns or raises without blocking, the task
379+
will be finished eagerly and will skip scheduling to the event loop.
380+
374381
.. versionchanged:: 3.8
375382
Added the *name* parameter.
376383

377384
.. versionchanged:: 3.11
378385
Added the *context* parameter.
379386

387+
.. versionchanged:: 3.13.3
388+
Added ``kwargs`` which always passes on ``kwargs`` such as the *eager_start*
389+
parameter and *name* parameter.
390+
391+
.. versionchanged:: 3.13.4
392+
Rolled back the change that passes on *name* and *context* (if it is None),
393+
passing on new keword arguments such as *eager_start* is still supported.
394+
380395
.. method:: loop.set_task_factory(factory)
381396

382397
Set a task factory that will be used by
@@ -388,6 +403,13 @@ Creating Futures and Tasks
388403
event loop, and *coro* is a coroutine object. The callable
389404
must pass on all *kwargs*, and return a :class:`asyncio.Task`-compatible object.
390405

406+
.. versionchanged:: 3.13.3
407+
Required that all *kwargs* are passed on to :class:`asyncio.Task`.
408+
409+
.. versionchanged:: 3.13.4
410+
*name* is no longer passed to task factories. *context* is no longer passed
411+
to task factories if it is ``None``.
412+
391413
.. method:: loop.get_task_factory()
392414

393415
Return a task factory or ``None`` if the default one is in use.

Doc/library/asyncio-task.rst

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,31 @@ Creating Tasks
238238

239239
-----------------------------------------------
240240

241-
.. function:: create_task(coro, *, name=None, context=None)
241+
.. function:: create_task(coro, *, name=None, context=None, eager_start=None, **kwargs)
242242

243243
Wrap the *coro* :ref:`coroutine <coroutine>` into a :class:`Task`
244244
and schedule its execution. Return the Task object.
245245

246+
The arguments shown above are merely the most common ones, described below
247+
The full function signature is largely the same as that of the
248+
:class:`Task` constructor (or factory) - all of the keyword arguments to
249+
this function are passed through to that interface, except *name*,
250+
or *context* if it is ``None``.
251+
246252
If *name* is not ``None``, it is set as the name of the task using
247253
:meth:`Task.set_name`.
248254

249255
An optional keyword-only *context* argument allows specifying a
250256
custom :class:`contextvars.Context` for the *coro* to run in.
251257
The current context copy is created when no *context* is provided.
252258

259+
An optional keyword-only *eager_start* argument allows eagerly starting
260+
the execution of the :class:`asyncio.Task` at task creation time.
261+
If set to ``True`` and the event loop is running, the task will start
262+
executing the coroutine immediately, until the first time the coroutine
263+
blocks. If the coroutine returns or raises without blocking, the task
264+
will be finished eagerly and will skip scheduling to the event loop.
265+
253266
The task is executed in the loop returned by :func:`get_running_loop`,
254267
:exc:`RuntimeError` is raised if there is no running loop in
255268
current thread.
@@ -290,6 +303,14 @@ Creating Tasks
290303
.. versionchanged:: 3.11
291304
Added the *context* parameter.
292305

306+
.. versionchanged:: 3.13.3
307+
Added ``kwargs`` which always passes on ``kwargs`` such as the *eager_start*
308+
parameter and *name* parameter.
309+
310+
.. versionchanged:: 3.13.4
311+
Rolled back the change that passes on *name* and *context* (if it is None),
312+
passing on new keword arguments such as *eager_start* is still supported.
313+
293314

294315
Task Cancellation
295316
=================
@@ -330,10 +351,10 @@ and reliable way to wait for all tasks in the group to finish.
330351

331352
.. versionadded:: 3.11
332353

333-
.. method:: create_task(coro, *, name=None, context=None)
354+
.. method:: create_task(coro, *, name=None, context=None, eager_start=None, **kwargs)
334355

335356
Create a task in this task group.
336-
The signature matches that of :func:`asyncio.create_task`.
357+
The signature matches that of :meth:`loop.create_task`.
337358
If the task group is inactive (e.g. not yet entered,
338359
already finished, or in the process of shutting down),
339360
we will close the given ``coro``.
@@ -342,6 +363,10 @@ and reliable way to wait for all tasks in the group to finish.
342363

343364
Close the given coroutine if the task group is not active.
344365

366+
.. versionchanged:: 3.13.3
367+
368+
Passes on all keyword arguments to :meth:`loop.create_task`
369+
345370
Example::
346371

347372
async def main():

0 commit comments

Comments
 (0)