Skip to content

Commit 2fff43d

Browse files
committed
Fix issue with lrutaskcache
1 parent 61ea5cb commit 2fff43d

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

async_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414

1515

16-
__version__ = "7.0.1"
16+
__version__ = "7.0.2"

async_utils/task_cache.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def wrapped(*args: P.args, **kwargs: P.kwargs) -> asyncio.Task[T]:
9494
return wrapper
9595

9696

97+
def _lru_evict(ttl: float, cache: LRU[Hashable, Any], key: Hashable, _ignored_task: object) -> None:
98+
asyncio.get_running_loop().call_later(ttl, cache.remove, key)
99+
100+
97101
def lrutaskcache(
98102
ttl: float | None = None, maxsize: int = 1024
99103
) -> Callable[[Callable[P, Coroutine[Any, Any, T]]], Callable[P, asyncio.Task[T]]]:
@@ -121,15 +125,7 @@ def wrapped(*args: P.args, **kwargs: P.kwargs) -> asyncio.Task[T]:
121125
except KeyError:
122126
internal_cache[key] = task = asyncio.create_task(coro(*args, **kwargs))
123127
if ttl is not None:
124-
# This results in internal_cache.pop(key, task) later
125-
# while avoiding a late binding issue with a lambda instead
126-
call_after_ttl = partial(
127-
asyncio.get_running_loop().call_later,
128-
ttl,
129-
internal_cache.remove,
130-
key,
131-
)
132-
task.add_done_callback(call_after_ttl)
128+
task.add_done_callback(partial(_lru_evict, ttl, internal_cache, key))
133129
return task
134130

135131
return wrapped

0 commit comments

Comments
 (0)