Skip to content

Commit b071971

Browse files
committed
Forward cache_close wait for bound methods
1 parent 33e1a7c commit b071971

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

async_lru/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,20 @@ def cache_clear(self) -> None:
333333
self.__wrapper.cache_clear()
334334

335335
async def cache_close(
336-
self, *, cancel: bool = False, return_exceptions: bool = True
336+
self,
337+
*,
338+
wait: bool = False,
339+
cancel: bool = False,
340+
return_exceptions: bool = True,
337341
) -> None:
338-
await self.__wrapper.cache_close()
342+
if cancel or return_exceptions is not True:
343+
warnings.warn(
344+
"cancel/return_exceptions are deprecated; use wait=True to allow tasks "
345+
"to finish and wait=False to cancel pending tasks.",
346+
DeprecationWarning,
347+
stacklevel=2,
348+
)
349+
await self.__wrapper.cache_close(wait=wait)
339350

340351
def cache_info(self) -> _CacheInfo:
341352
return self.__wrapper.cache_info()

tests/test_close.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,28 @@ async def coro(val: int) -> int:
4242

4343
# double call is no-op
4444
await coro.cache_close()
45+
46+
47+
async def test_cache_close_wait_bound_method(check_lru: Callable[..., None]) -> None:
48+
class Foo:
49+
@alru_cache()
50+
async def coro(self, val: int) -> int:
51+
await asyncio.sleep(0.05)
52+
return val
53+
54+
foo = Foo()
55+
inputs = [1, 2, 3]
56+
57+
coros = [foo.coro(v) for v in inputs]
58+
gather = asyncio.gather(*coros)
59+
60+
await asyncio.sleep(0.01)
61+
62+
# wait=True should allow tasks to finish (no cancellation)
63+
await foo.coro.cache_close(wait=True)
64+
65+
results = await gather
66+
assert results == inputs
67+
68+
check_lru(foo.coro, hits=0, misses=3, cache=3, tasks=0)
69+
assert foo.coro.cache_parameters()["closed"]

0 commit comments

Comments
 (0)