From a76afc45d5f510159357ffcdb2b4e00880584d20 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Wed, 6 Jul 2022 10:48:12 +0200 Subject: [PATCH 1/2] Update timeout helper to use get_running_loop(). --- asgiref/timeout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asgiref/timeout.py b/asgiref/timeout.py index 46cdc798..f0ce7b42 100644 --- a/asgiref/timeout.py +++ b/asgiref/timeout.py @@ -35,7 +35,7 @@ def __init__( ) -> None: self._timeout = timeout if loop is None: - loop = asyncio.get_event_loop() + loop = asyncio.get_running_loop() self._loop = loop self._task = None # type: Optional[asyncio.Task[Any]] self._cancelled = False From 42bf0eb276980576fc4715a5a2025d04d7eb930f Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Mon, 8 Aug 2022 08:51:48 +0200 Subject: [PATCH 2/2] Deprecated loop argument to timeout(). --- CHANGELOG.txt | 5 +++++ asgiref/timeout.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 0c2c2063..0a4f1c85 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,11 @@ UNRELEASED ---------- +* The ``loop`` argument to ``asgiref.timeout.timeout`` is deprecated. As per other + ``asyncio`` based APIs, the running event loop is used by default. Note that + ``asyncio`` provides timeout utilities from Python 3.11, and these should be + preferred where available. + * Support for the ``ASGI_THREADS`` environment variable, used by ``SyncToAsync``, is removed. In general, a running event-loop is not available to `asgiref` at import time, and so the default thread pool diff --git a/asgiref/timeout.py b/asgiref/timeout.py index f0ce7b42..fd5381d0 100644 --- a/asgiref/timeout.py +++ b/asgiref/timeout.py @@ -7,6 +7,7 @@ import asyncio +import warnings from types import TracebackType from typing import Any # noqa from typing import Optional, Type @@ -36,6 +37,10 @@ def __init__( self._timeout = timeout if loop is None: loop = asyncio.get_running_loop() + else: + warnings.warn( + """The loop argument to timeout() is deprecated.""", DeprecationWarning + ) self._loop = loop self._task = None # type: Optional[asyncio.Task[Any]] self._cancelled = False