From 4db094a569f6dad2cf384722fe836c4a94ac96fb Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Fri, 15 May 2020 19:58:25 +0200 Subject: [PATCH 1/6] timeout can be used as function decorator --- README.rst | 7 +++++++ async_timeout/__init__.py | 6 ++++++ tests/test_timeout.py | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.rst b/README.rst index f1174d4..0561947 100644 --- a/README.rst +++ b/README.rst @@ -37,6 +37,13 @@ that cancels a block on *timeout* expiring:: *timeout* parameter could be ``None`` for skipping timeout functionality. +`timeout` can be also used as a decorator:: + + @timeout(1.5) + async def possibly_long_running(): + pass + + Alternatively, ``timeout_at(when)`` can be used for scheduling at the absolute time:: diff --git a/async_timeout/__init__.py b/async_timeout/__init__.py index e86b319..c4d7452 100644 --- a/async_timeout/__init__.py +++ b/async_timeout/__init__.py @@ -128,6 +128,12 @@ async def __aexit__( self._do_exit(exc_type) return None + def __call__(self, f): + async def inner(*args, **kargs): + with self: + return await f(*args, **kargs) + return inner + @property def expired(self) -> bool: """Is timeout expired during execution?""" diff --git a/tests/test_timeout.py b/tests/test_timeout.py index 28ad4be..41a0603 100644 --- a/tests/test_timeout.py +++ b/tests/test_timeout.py @@ -26,6 +26,26 @@ async def long_running_task(): assert canceled_raised, "CancelledError was not raised" +@pytest.mark.asyncio +async def test_timeout_as_decorator(): + canceled_raised = False + + @timeout(0.01) + async def long_running_task(arg, *, karg): + assert arg == 1 + assert karg == 2 + try: + await asyncio.sleep(10) + except asyncio.CancelledError: + nonlocal canceled_raised + canceled_raised = True + raise + + with pytest.raises(asyncio.TimeoutError): + await long_running_task(1, karg=2) + assert canceled_raised, "CancelledError was not raised" + + @pytest.mark.asyncio async def test_timeout_finish_in_time(): async def long_running_task(): From bf7597768793def6cb95b418e70ba8ea4a390934 Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Fri, 15 May 2020 19:59:51 +0200 Subject: [PATCH 2/6] readme corrected --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 0561947..a6202be 100644 --- a/README.rst +++ b/README.rst @@ -37,7 +37,7 @@ that cancels a block on *timeout* expiring:: *timeout* parameter could be ``None`` for skipping timeout functionality. -`timeout` can be also used as a decorator:: +Another possibility is to use ``timeout`` as function decorator:: @timeout(1.5) async def possibly_long_running(): From ef7959979ef8674d88d9bacd4e9b3f1e029d4620 Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Fri, 15 May 2020 20:07:15 +0200 Subject: [PATCH 3/6] changelog --- CHANGES.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 3e6db89..508004b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,11 @@ CHANGES ======= +Unreleased +---------- + +* Add posibility to use timeout as function decorator + 4.0.0 (2019-11-XX) ------------------ From db987d9bc42156d1eec205dd6ffbe3dde241fe55 Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Thu, 25 Jun 2020 08:34:23 +0200 Subject: [PATCH 4/6] fmt --- async_timeout/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/async_timeout/__init__.py b/async_timeout/__init__.py index c4d7452..73913ca 100644 --- a/async_timeout/__init__.py +++ b/async_timeout/__init__.py @@ -132,6 +132,7 @@ def __call__(self, f): async def inner(*args, **kargs): with self: return await f(*args, **kargs) + return inner @property From 3fd29b352ac7e5380d652319e790f6492d61bd5e Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Thu, 25 Jun 2020 08:39:42 +0200 Subject: [PATCH 5/6] changelog updated --- CHANGES.rst | 5 ----- CHANGES/141.feature | 1 + 2 files changed, 1 insertion(+), 5 deletions(-) create mode 100644 CHANGES/141.feature diff --git a/CHANGES.rst b/CHANGES.rst index 508004b..3e6db89 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,11 +1,6 @@ CHANGES ======= -Unreleased ----------- - -* Add posibility to use timeout as function decorator - 4.0.0 (2019-11-XX) ------------------ diff --git a/CHANGES/141.feature b/CHANGES/141.feature new file mode 100644 index 0000000..8c77334 --- /dev/null +++ b/CHANGES/141.feature @@ -0,0 +1 @@ +`timeout` can now also be used as function decorator. \ No newline at end of file From 1dfe0d572d052d95f05f8234e2a663352108c1cf Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Thu, 29 Oct 2020 14:53:48 +0200 Subject: [PATCH 6/6] Update async_timeout/__init__.py --- async_timeout/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_timeout/__init__.py b/async_timeout/__init__.py index 73913ca..51c5198 100644 --- a/async_timeout/__init__.py +++ b/async_timeout/__init__.py @@ -130,7 +130,7 @@ async def __aexit__( def __call__(self, f): async def inner(*args, **kargs): - with self: + async with self: return await f(*args, **kargs) return inner