Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Python 3.12-style wait_for #1086

Merged
merged 1 commit into from
Oct 9, 2023
Merged
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
87 changes: 87 additions & 0 deletions asyncpg/_asyncio_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 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

if sys.version_info < (3, 11):
from async_timeout import timeout as timeout_ctx
else:
from asyncio import timeout as timeout_ctx


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 asyncio.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 = asyncio.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)
20 changes: 5 additions & 15 deletions asyncpg/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0


import asyncio
import pathlib
import platform
import typing
import sys


SYSTEM = platform.uname().system
Expand Down Expand Up @@ -49,17 +49,7 @@ async def wait_closed(stream):
pass


# Workaround for https://bugs.python.org/issue37658
async def wait_for(fut, timeout):
if timeout is None:
return await fut

fut = asyncio.ensure_future(fut)

try:
return await asyncio.wait_for(fut, timeout)
except asyncio.CancelledError:
if fut.done():
return fut.result()
else:
raise
if sys.version_info < (3, 12):
from ._asyncio_compat import wait_for as wait_for # noqa: F401
else:
from asyncio import wait_for as wait_for # noqa: F401
6 changes: 3 additions & 3 deletions asyncpg/protocol/protocol.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ cdef class BaseProtocol(CoreProtocol):

while more:
with timer:
await asyncio.wait_for(
await compat.wait_for(
self.writing_allowed.wait(),
timeout=timer.get_remaining_budget())
# On Windows the above event somehow won't allow context
Expand Down Expand Up @@ -383,7 +383,7 @@ cdef class BaseProtocol(CoreProtocol):
if buffer:
try:
with timer:
await asyncio.wait_for(
await compat.wait_for(
sink(buffer),
timeout=timer.get_remaining_budget())
except (Exception, asyncio.CancelledError) as ex:
Expand Down Expand Up @@ -511,7 +511,7 @@ cdef class BaseProtocol(CoreProtocol):
with timer:
await self.writing_allowed.wait()
with timer:
chunk = await asyncio.wait_for(
chunk = await compat.wait_for(
iterator.__anext__(),
timeout=timer.get_remaining_budget())
self._write_copy_data_msg(chunk)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Database :: Front-Ends",
]
dependencies = [
'async_timeout>=4.0.3; python_version < "3.12.0"'
]

[project.urls]
github = "https://github.com/MagicStack/asyncpg"
Expand Down