清除 Python 中的所有 lru_cache

kyr*_*nia 6 python caching lru

我在 python 中有带有 lru_cache 缓存的函数,例如

 @lru_cache(maxsize=None)
 def my_function():
    ...
Run Code Online (Sandbox Code Playgroud)

虽然我可以单独清除缓存,例如my_function.cache_clear()有没有办法一次清除每个函数的缓存?[我在想,也许有一种方法可以返回内存中加载的所有函数名称,然后循环遍历它们以清除每个函数的缓存]。

我特别希望作为回退的一部分来实现,例如在我的机器上 90% 的内存被使用的情况下。

Bla*_*ift 9

也许有一种方法可以返回加载到内存中的所有函数名称,然后循环遍历它们并从每个函数中清除缓存

是的,这也是可能的:

import functools
import gc

gc.collect()
wrappers = [
    a for a in gc.get_objects() 
    if isinstance(a, functools._lru_cache_wrapper)]

for wrapper in wrappers:
    wrapper.cache_clear()
Run Code Online (Sandbox Code Playgroud)

我特别希望作为回退的一部分来实现,例如在我的机器上 90% 的内存被使用的情况下。

这是您在正常情况下可能不会使用的代码,但对调试很有用。

在我的特殊情况下,我想从 matplotlib 中清除一些悬空的文件句柄(以便专注于我自己的松散文件句柄),因此无法使用 Alex Hall 的解决方案。


Ale*_*all 8

您可以创建一个修改过的装饰器,它也会记录缓存的函数:

cached_functions = []

def clearable_lru_cache(*args, **kwargs):
    def decorator(func):
        func = lru_cache(*args, **kwargs)(func)
        cached_functions.append(func)
        return func

    return decorator

def clear_all_cached_functions():
    for func in cached_functions:
        func.cache_clear()
Run Code Online (Sandbox Code Playgroud)

如果你真的想要,你也可以使用猴子补丁来替换原来的装饰器。

测试:

@clearable_lru_cache()
def foo(x):
    print('foo', x)

@clearable_lru_cache()
def bar(x):
    print('bar', x)

for i in [1, 2]:
    for j in [1, 2]:
        print('Calling functions')
        for k in [1, 2, 3]:
            for f in [foo, bar]:
                f(k)
        print('Functions called - if you saw nothing they were cached')
    print('Clearing cache')
    clear_all_cached_functions()
Run Code Online (Sandbox Code Playgroud)

输出:

Calling functions
foo 1
bar 1
foo 2
bar 2
foo 3
bar 3
Functions called - if you saw nothing they were cached
Calling functions
Functions called - if you saw nothing they were cached
Clearing cache
Calling functions
foo 1
bar 1
foo 2
bar 2
foo 3
bar 3
Functions called - if you saw nothing they were cached
Calling functions
Functions called - if you saw nothing they were cached
Clearing cache
Run Code Online (Sandbox Code Playgroud)