mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
(Merge 3.4) Issue #22112, asyncio doc: replace loop.create_task(coro) with
asyncio.async(coro), mention that asyncio.async() can be used to scheduler a coroutine, and make it clear that create_task() is only available in Python 3.4.2 and later.
This commit is contained in:
commit
03f536ec2e
3 changed files with 19 additions and 16 deletions
|
@ -147,9 +147,8 @@ Coroutines
|
||||||
interoperability. In this case, the result type is a subclass of
|
interoperability. In this case, the result type is a subclass of
|
||||||
:class:`Task`.
|
:class:`Task`.
|
||||||
|
|
||||||
.. seealso::
|
This method was added in Python 3.4.2. Use the :func:`async` function to
|
||||||
|
support also older Python versions.
|
||||||
The :meth:`async` function.
|
|
||||||
|
|
||||||
.. versionadded:: 3.4.2
|
.. versionadded:: 3.4.2
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ Stream functions
|
||||||
:class:`StreamWriter` object. The *client_connected_cb* parameter can
|
:class:`StreamWriter` object. The *client_connected_cb* parameter can
|
||||||
either be a plain callback function or a :ref:`coroutine function
|
either be a plain callback function or a :ref:`coroutine function
|
||||||
<coroutine>`; if it is a coroutine function, it will be automatically
|
<coroutine>`; if it is a coroutine function, it will be automatically
|
||||||
wrapped in a future using the :meth:`BaseEventLoop.create_task` method.
|
converted into a :class:`Task`.
|
||||||
|
|
||||||
The rest of the arguments are all the usual arguments to
|
The rest of the arguments are all the usual arguments to
|
||||||
:meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most
|
:meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most
|
||||||
|
|
|
@ -52,7 +52,9 @@ generator object, which doesn't do anything until you iterate over it.
|
||||||
In the case of a coroutine object, there are two basic ways to start
|
In the case of a coroutine object, there are two basic ways to start
|
||||||
it running: call ``yield from coroutine`` from another coroutine
|
it running: call ``yield from coroutine`` from another coroutine
|
||||||
(assuming the other coroutine is already running!), or schedule its execution
|
(assuming the other coroutine is already running!), or schedule its execution
|
||||||
using the :meth:`BaseEventLoop.create_task` method.
|
using the :func:`async` function or the :meth:`BaseEventLoop.create_task`
|
||||||
|
method.
|
||||||
|
|
||||||
|
|
||||||
Coroutines (and tasks) can only run when the event loop is running.
|
Coroutines (and tasks) can only run when the event loop is running.
|
||||||
|
|
||||||
|
@ -256,7 +258,7 @@ Example combining a :class:`Future` and a :ref:`coroutine function
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
future = asyncio.Future()
|
future = asyncio.Future()
|
||||||
loop.create_task(slow_operation(future))
|
asyncio.async(slow_operation(future))
|
||||||
loop.run_until_complete(future)
|
loop.run_until_complete(future)
|
||||||
print(future.result())
|
print(future.result())
|
||||||
loop.close()
|
loop.close()
|
||||||
|
@ -292,7 +294,7 @@ flow::
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
future = asyncio.Future()
|
future = asyncio.Future()
|
||||||
loop.create_task(slow_operation(future))
|
asyncio.async(slow_operation(future))
|
||||||
future.add_done_callback(got_result)
|
future.add_done_callback(got_result)
|
||||||
try:
|
try:
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
@ -339,8 +341,8 @@ Task
|
||||||
<coroutine>` did not complete. It is probably a bug and a warning is
|
<coroutine>` did not complete. It is probably a bug and a warning is
|
||||||
logged: see :ref:`Pending task destroyed <asyncio-pending-task-destroyed>`.
|
logged: see :ref:`Pending task destroyed <asyncio-pending-task-destroyed>`.
|
||||||
|
|
||||||
Don't create directly :class:`Task` instances: use the
|
Don't create directly :class:`Task` instances: use the :func:`async`
|
||||||
:meth:`BaseEventLoop.create_task` method.
|
function or the :meth:`BaseEventLoop.create_task` method.
|
||||||
|
|
||||||
.. classmethod:: all_tasks(loop=None)
|
.. classmethod:: all_tasks(loop=None)
|
||||||
|
|
||||||
|
@ -424,9 +426,9 @@ Example executing 3 tasks (A, B, C) in parallel::
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
tasks = [
|
tasks = [
|
||||||
loop.create_task(factorial("A", 2)),
|
asyncio.async(factorial("A", 2)),
|
||||||
loop.create_task(factorial("B", 3)),
|
asyncio.async(factorial("B", 3)),
|
||||||
loop.create_task(factorial("C", 4))]
|
asyncio.async(factorial("C", 4))]
|
||||||
loop.run_until_complete(asyncio.wait(tasks))
|
loop.run_until_complete(asyncio.wait(tasks))
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
|
@ -475,11 +477,14 @@ Task functions
|
||||||
|
|
||||||
.. function:: async(coro_or_future, \*, loop=None)
|
.. function:: async(coro_or_future, \*, loop=None)
|
||||||
|
|
||||||
Wrap a :ref:`coroutine object <coroutine>` in a future using the
|
Wrap a :ref:`coroutine object <coroutine>` in a future.
|
||||||
:meth:`BaseEventLoop.create_task` method.
|
|
||||||
|
|
||||||
If the argument is a :class:`Future`, it is returned directly.
|
If the argument is a :class:`Future`, it is returned directly.
|
||||||
|
|
||||||
|
.. seealso::
|
||||||
|
|
||||||
|
The :meth:`BaseEventLoop.create_task` method.
|
||||||
|
|
||||||
.. function:: gather(\*coros_or_futures, loop=None, return_exceptions=False)
|
.. function:: gather(\*coros_or_futures, loop=None, return_exceptions=False)
|
||||||
|
|
||||||
Return a future aggregating results from the given coroutine objects or
|
Return a future aggregating results from the given coroutine objects or
|
||||||
|
@ -595,8 +600,7 @@ Task functions
|
||||||
to complete with timeout. If *timeout* is ``None``, block until the future
|
to complete with timeout. If *timeout* is ``None``, block until the future
|
||||||
completes.
|
completes.
|
||||||
|
|
||||||
Coroutine objects are wrapped in a future using the
|
Coroutine will be wrapped in :class:`Task`.
|
||||||
:meth:`BaseEventLoop.create_task` method.
|
|
||||||
|
|
||||||
Returns result of the Future or coroutine. When a timeout occurs, it
|
Returns result of the Future or coroutine. When a timeout occurs, it
|
||||||
cancels the task and raises :exc:`asyncio.TimeoutError`. To avoid the task
|
cancels the task and raises :exc:`asyncio.TimeoutError`. To avoid the task
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue