Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -843,17 +843,16 @@ Timeouts
Wait for the *fut* :ref:`awaitable <asyncio-awaitables>`
to complete with a timeout.

If *fut* is a coroutine it is automatically scheduled as a Task.
If *fut* is a coroutine, it is awaited directly rather than being wrapped
in a :class:`Task`, unless *timeout* is zero or negative.

*timeout* can either be ``None`` or a float or int number of seconds
to wait for. If *timeout* is ``None``, block until the future
completes.

If a timeout occurs, it cancels the task and raises
:exc:`TimeoutError`.
If a timeout occurs, it cancels *fut* and raises :exc:`TimeoutError`.

To avoid the task :meth:`cancellation <Task.cancel>`,
wrap it in :func:`shield`.
To prevent *fut* from being cancelled, wrap it in :func:`shield`.

The function will wait until the future is actually cancelled,
so the total wait time may exceed the *timeout*. If an exception
Expand Down Expand Up @@ -894,6 +893,10 @@ Timeouts
.. versionchanged:: 3.11
Raises :exc:`TimeoutError` instead of :exc:`asyncio.TimeoutError`.

.. versionchanged:: 3.12
Implemented using :func:`asyncio.timeout`, a coroutine passed as *fut*
is no longer wrapped in a :class:`Task` when *timeout* is positive.


Waiting primitives
==================
Expand Down
11 changes: 6 additions & 5 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,16 @@ def _release_waiter(waiter, *args):
async def wait_for(fut, timeout):
"""Wait for the single Future or coroutine to complete, with timeout.
Coroutine will be wrapped in Task.
A coroutine is awaited directly rather than being wrapped in a Task,
unless timeout is zero or negative.
Returns result of the Future or coroutine. When a timeout occurs,
it cancels the task and raises TimeoutError. To avoid the task
cancellation, wrap it in shield().
it cancels fut and raises TimeoutError. To prevent fut from being
cancelled, wrap it in shield().
If the wait is cancelled, the task is also cancelled.
If the wait is cancelled, fut is also cancelled.
If the task suppresses the cancellation and returns a value instead,
If fut suppresses the cancellation and returns a value instead,
that value is returned.
This function is a coroutine.
Expand Down
Loading