将缓存存储到Python> = 3.2中的functools.lru_cache文件中 [英] Store the cache to a file functools.lru_cache in Python >= 3.2
问题描述
我在Python 3.3中使用@functools.lru_cache
.我想将缓存保存到文件中,以便在重新启动程序时将其还原.我该怎么办?
I'm using @functools.lru_cache
in Python 3.3. I would like to save the cache to a file, in order to restore it when the program will be restarted. How could I do?
编辑1 可能的解决方案:我们需要腌制任何种类的可呼叫食品
问题酸洗__closure__
:
_pickle.PicklingError: Can't pickle <class 'cell'>: attribute lookup builtins.cell failed
如果我尝试在没有该功能的情况下恢复该功能,则会得到:
If I try to restore the function without it, I get:
TypeError: arg 5 (closure) must be tuple
推荐答案
您无法使用lru_cache
做您想做的事,因为它不提供访问缓存的API,并且可能会用C重写在将来的版本中.如果您确实要保存缓存,则必须使用其他解决方案来访问缓存.
You can't do what you want using lru_cache
, since it doesn't provide an API to access the cache, and it might be rewritten in C in future releases. If you really want to save the cache you have to use a different solution that gives you access to the cache.
编写自己的缓存非常简单.例如:
It's simple enough to write a cache yourself. For example:
from functools import wraps
def cached(func):
func.cache = {}
@wraps(func)
def wrapper(*args):
try:
return func.cache[args]
except KeyError:
func.cache[args] = result = func(*args)
return result
return wrapper
然后可以将其用作装饰器:
You can then apply it as a decorator:
>>> @cached
... def fibonacci(n):
... if n < 2:
... return n
... return fibonacci(n-1) + fibonacci(n-2)
...
>>> fibonacci(100)
354224848179261915075L
并检索cache
:
>>> fibonacci.cache
{(32,): 2178309, (23,): 28657, ... }
然后您可以根据需要腌制/解开缓存并加载:
You can then pickle/unpickle the cache as you please and load it with:
fibonacci.cache = pickle.load(cache_file_object)
我在python的问题跟踪器中找到了功能请求,以将转储/加载添加到lru_cache
,但是它没有被接受/实现.也许将来可能会通过lru_cache
内置对这些操作的支持.
I found a feature request in python's issue tracker to add dumps/loads to lru_cache
, but it wasn't accepted/implemented. Maybe in the future it will be possible to have built-in support for these operations via lru_cache
.
这篇关于将缓存存储到Python> = 3.2中的functools.lru_cache文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!