Ame*_*ina 6 python pandas joblib
假设我使用Joblib设置memoization如下(使用此处提供的解决方案):
from tempfile import mkdtemp
cachedir = mkdtemp()
from joblib import Memory
memory = Memory(cachedir=cachedir, verbose=0)
@memory.cache
def run_my_query(my_query)
...
return df
Run Code Online (Sandbox Code Playgroud)
并说我定义了几个查询,query_1并且query_2它们都需要很长时间才能运行.
我理解,使用代码:
使用任一查询的第二次调用将使用memoized输出,即:
run_my_query(query_1)
run_my_query(query_1) # <- Uses cached output
run_my_query(query_2)
run_my_query(query_2) # <- Uses cached output
Run Code Online (Sandbox Code Playgroud)我可以memory.clear()用来删除整个缓存目录
但是,如果我只想对其中一个查询(例如)重新执行 memoization 而不强制删除其他查询,该怎么办?query_2
该库似乎不支持部分擦除缓存。
您可以将缓存、功能分成两对:
from tempfile import mkdtemp
from joblib import Memory
memory1 = Memory(cachedir=mkdtemp(), verbose=0)
memory2 = Memory(cachedir=mkdtemp(), verbose=0)
@memory1.cache
def run_my_query1()
# run query_1
return df
@memory2.cache
def run_my_query2()
# run query_2
return df
Run Code Online (Sandbox Code Playgroud)
现在,您可以有选择地清除缓存:
memory2.clear()
Run Code Online (Sandbox Code Playgroud)
看到 behzad.nouri 的评论后更新:
您可以使用call装饰函数的方法。但正如您在下面的示例中看到的,返回值与正常调用不同。你应该照顾它。
>>> import tempfile
>>> import joblib
>>> memory = joblib.Memory(cachedir=tempfile.mkdtemp(), verbose=0)
>>> @memory.cache
... def run(x):
... print('called with {}'.format(x)) # for debug
... return x
...
>>> run(1)
called with 1
1
>>> run(2)
called with 2
2
>>> run(3)
called with 3
3
>>> run(2) # Cached
2
>>> run.call(2) # Force call of the original function
called with 2
(2, {'duration': 0.0011069774627685547, 'input_args': {'x': '2'}})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
338 次 |
| 最近记录: |