Marten Lienen added the comment:
As suggested, I have extracted the code and tests into a package on PyPI:
https://pypi.org/project/cached_method/
With this I will close this issue. "third party" sounds about right as the
Resolution setting.
@eric.araujo This feature is abo
Marten Lienen added the comment:
The lru_cache can trigger infinite recursion if it is used to cache a hash
computation because the cache look-up itself requires the hash.
from functools import lru_cache
class CachedHash:
@lru_cache
def __hash__(self):
# Expensive
Marten Lienen added the comment:
Central control over the cache and its parameters is definitely a big plus. In
my use case, the objects hold references to large blocks of GPU memory that
should be freed as soon as possible. Additionally, I use cached_method to cache
expensive hash
Marten Lienen added the comment:
However, then the user gets error messages mentioning cached_property when they
are actually using cached_method.
--
___
Python tracker
<https://bugs.python.org/issue45
Marten Lienen added the comment:
An implementation based on cached_property (that therefore also inherits its
locking problem) that does not create a circular reference and copies
attributes (docs etc.) from the decorated function would be as follows (based
on the WeaklyBoundMethod from
Marten Lienen added the comment:
@serhiy.storchaka The simple implementation is very simple but does not allow
overwriting the arguments to `lru_cache` and, probably more importantly,
creates a circular reference on `self`, I believe
Marten Lienen added the comment:
@AlexWaygood No, I was not aware of the problems. I have updated the PR by
removing the lock entirely. Since the function has to be cacheable, it should
be idempotent anyway, so that executing it multiple times in parallel does not
make a program incorrect
Change by Marten Lienen :
--
keywords: +patch
pull_requests: +27462
stage: -> patch review
pull_request: https://github.com/python/cpython/pull/29191
___
Python tracker
<https://bugs.python.org/issu
New submission from Marten Lienen :
There should be a `cached_method` equivalent to `cached_property` so that
methods of long-lived objects can be cached easily.
--
components: Library (Lib)
messages: 404870
nosy: martenlienen
priority: normal
severity: normal
status: open
title