-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to Python 3.12-style
wait_for
`wait_for` has been a mess with respect to cancellations consistently in `asyncio`. Hopefully the approach taken in Python 3.12 solves the issues, so adopt that instead of trying to "fix" `wait_for` with wrappers on older Pythons. Use `async_timeout` as a polyfill on pre-3.11 Python. Closes: #1056 Closes: #1052 Fixes: #955
- Loading branch information
Showing
4 changed files
with
104 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Backports from Python/Lib/asyncio for older Pythons | ||
# | ||
# Copyright (c) 2001-2023 Python Software Foundation; All Rights Reserved | ||
# | ||
# SPDX-License-Identifier: PSF-2.0 | ||
|
||
|
||
import asyncio | ||
import functools | ||
import sys | ||
|
||
from . import events | ||
from . import exceptions | ||
|
||
|
||
if sys.version_info < (3, 11): | ||
from async_timeout import timeout as timeout_ctx | ||
else: | ||
from asyncio import timeout as timeout_ctx | ||
|
||
from async_timeout import timeout as timeout_ctx_2 | ||
|
||
|
||
async def wait_for(fut, timeout): | ||
"""Wait for the single Future or coroutine to complete, with timeout. | ||
Coroutine will be wrapped in Task. | ||
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(). | ||
If the wait is cancelled, the task is also cancelled. | ||
If the task supresses the cancellation and returns a value instead, | ||
that value is returned. | ||
This function is a coroutine. | ||
""" | ||
# The special case for timeout <= 0 is for the following case: | ||
# | ||
# async def test_waitfor(): | ||
# func_started = False | ||
# | ||
# async def func(): | ||
# nonlocal func_started | ||
# func_started = True | ||
# | ||
# try: | ||
# await asyncio.wait_for(func(), 0) | ||
# except asyncio.TimeoutError: | ||
# assert not func_started | ||
# else: | ||
# assert False | ||
# | ||
# asyncio.run(test_waitfor()) | ||
|
||
if timeout is not None and timeout <= 0: | ||
fut = asyncio.ensure_future(fut) | ||
|
||
if fut.done(): | ||
return fut.result() | ||
|
||
await _cancel_and_wait(fut) | ||
try: | ||
return fut.result() | ||
except exceptions.CancelledError as exc: | ||
raise TimeoutError from exc | ||
|
||
async with timeout_ctx(timeout): | ||
return await fut | ||
|
||
|
||
async def _cancel_and_wait(fut): | ||
"""Cancel the *fut* future or task and wait until it completes.""" | ||
|
||
loop = events.get_running_loop() | ||
waiter = loop.create_future() | ||
cb = functools.partial(_release_waiter, waiter) | ||
fut.add_done_callback(cb) | ||
|
||
try: | ||
fut.cancel() | ||
# We cannot wait on *fut* directly to make | ||
# sure _cancel_and_wait itself is reliably cancellable. | ||
await waiter | ||
finally: | ||
fut.remove_done_callback(cb) | ||
|
||
|
||
def _release_waiter(waiter, *args): | ||
if not waiter.done(): | ||
waiter.set_result(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters