diff options
author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2017-12-15 05:04:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-15 05:04:38 (GMT) |
commit | f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847 (patch) | |
tree | 7c622fa99ba081586a655b1744dae0a46b3a0f95 /Doc | |
parent | 19a44f63c738388ef3c8515348b4ffc061dfd627 (diff) | |
download | cpython-f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847.zip cpython-f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847.tar.gz cpython-f74ef458ab1f502e4e60bd1502ac1dc0d2cb3847.tar.bz2 |
bpo-32311: Implement asyncio.create_task() shortcut (#4848)
* Implement functionality
* Add documentation
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/asyncio-task.rst | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 0d0569f0..72fae5e 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -371,10 +371,21 @@ with the result. Task ---- +.. function:: create_task(coro) + + Wrap a :ref:`coroutine <coroutine>` *coro* into a task and schedule + its execution. Return the task object. + + The task is executed in :func:`get_running_loop` context, + :exc:`RuntimeError` is raised if there is no running loop in + current thread. + + .. versionadded:: 3.7 + .. class:: Task(coro, \*, loop=None) - Schedule the execution of a :ref:`coroutine <coroutine>`: wrap it in a - future. A task is a subclass of :class:`Future`. + A unit for concurrent running of :ref:`coroutines <coroutine>`, + subclass of :class:`Future`. A task is responsible for executing a coroutine object in an event loop. If the wrapped coroutine yields from a future, the task suspends the execution @@ -399,7 +410,7 @@ Task <coroutine>` did not complete. It is probably a bug and a warning is logged: see :ref:`Pending task destroyed <asyncio-pending-task-destroyed>`. - Don't directly create :class:`Task` instances: use the :func:`ensure_future` + Don't directly create :class:`Task` instances: use the :func:`create_task` function or the :meth:`AbstractEventLoop.create_task` method. This class is :ref:`not thread safe <asyncio-multithreading>`. @@ -547,9 +558,15 @@ Task functions .. versionchanged:: 3.5.1 The function accepts any :term:`awaitable` object. + .. note:: + + :func:`create_task` (added in Python 3.7) is the preferable way + for spawning new tasks. + .. seealso:: - The :meth:`AbstractEventLoop.create_task` method. + The :func:`create_task` function and + :meth:`AbstractEventLoop.create_task` method. .. function:: wrap_future(future, \*, loop=None) |