Agreed. A possible solution is that if given weakref=True and a non-weakreffable key, an exception will be raised when the function attempts to save it in cache.
Paul, I'll see if I can give an example use case tomorrow. On Thu, Oct 15, 2020, 21:38 Irit Katriel <iritkatr...@yahoo.com> wrote: > We have a couple of weak caches In our system, so I see the use case. > > I don’t like the idea of silently reverting to a strong ref though. I’ve > had bad experiences with systems that try to be too helpful in this manner. > It seems like a good idea until it becomes a big problem, and then you have > interesting debates about who’s fault it is. > > Irit > > On 15 Oct 2020, at 18:53, Ram Rachum <r...@rachum.com> wrote: > > > Hi everyone, > > For many years, I've used a `cache` decorator that I built > <https://github.com/cool-RR/python_toolbox/blob/master/python_toolbox/caching/decorators.py#L36> > for caching Python functions. Then `functools.lru_cache` was implemented, > which is much more standard. However, as far as I know, it holds normal > references to its keys, rather than weak references. This means that it can > cause memory leaks when it's storing items that don't have any references > elsewhere. This often makes me reluctant to use it. > > What do you think about supporting weakrefs in for keys lru_cache? > > If I remember correctly, the main difficulty was that not all keys are of > a type that can be weakreffed. If I remember correctly again, I've solved > this by including logic that attempts a weakref when possible, and degrades > to a strong ref. What do you think about that? > > > Thanks, > Ram. > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/Q6DT72QLFRE23Z7DT3B6KYKQEOC4R6KO/ > Code of Conduct: http://python.org/psf/codeofconduct/ > >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DPB6GTIWPIPHZFVC43BITMDA23DTMYO3/ Code of Conduct: http://python.org/psf/codeofconduct/