diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 596cb7565a66e7d..f0ad36096dd8570 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -843,17 +843,16 @@ Timeouts Wait for the *fut* :ref:`awaitable ` 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 `, - 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 @@ -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 ================== diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 7889d4793a5dec3..129bcf7c838a096 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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.