如何使用joblib.Memory缓存Python类的成员函数的输出

mot*_*m79 8 python memory caching decorator joblib

我想使用joblib.Memory库来缓存类的成员函数的输出.这是一个示例代码:

import joblib
import numpy as np

mem = joblib.Memory(cachedir='/tmp', verbose=1)


@mem.cache
def my_sum(x):
    return np.sum(x)


class TestClass(object):
    def __init__(self):
        pass

    @mem.cache
    def my_sum(self, x):
        return np.sum(x)


if __name__ == '__main__':
    x = np.array([1, 2, 3, 4])
    a = TestClass()
    print a.my_sum(x)  # does not work
    print my_sum(x) # works fine
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

/nfs/sw/anaconda2/lib/python2.7/site-packages/joblib/memory.pyc in _get_output_dir(self, *args, **kwargs)
    512             of the function called with the given arguments.
    513         """
--> 514         argument_hash = self._get_argument_hash(*args, **kwargs)
    515         output_dir = os.path.join(self._get_func_dir(self.func),
    516                                   argument_hash)

/nfs/sw/anaconda2/lib/python2.7/site-packages/joblib/memory.pyc in _get_argument_hash(self, *args, **kwargs)
    505     def _get_argument_hash(self, *args, **kwargs):
    506         return hashing.hash(filter_args(self.func, self.ignore,
--> 507                                          args, kwargs),
    508                              coerce_mmap=(self.mmap_mode is not None))
    509 

/nfs/sw/anaconda2/lib/python2.7/site-packages/joblib/func_inspect.pyc in filter_args(func, ignore_lst, args, kwargs)
    228                            repr(args)[1:-1],
    229                            ', '.join('%s=%s' % (k, v)
--> 230                                     for k, v in kwargs.items())
    231                            )
    232                         )

ValueError: Wrong number of arguments for my_sum(self, x):
     my_sum(array([1, 2, 3, 4]), ) was called.
Run Code Online (Sandbox Code Playgroud)

有没有办法使用内存或任何其他装饰器缓存类的成员函数?

小智 14

以下摘录摘自https://pythonhosted.org/joblib/memory.html#gotchas

缓存方法:您无法在类定义中修饰方法,因为在实例化类时,第一个参数(self)被绑定,并且Memory对象不再可访问.以下代码不起作用:

class Foo(object):

class Foo(object):

    @mem.cache  # WRONG
    def method(self, args):
        pass
Run Code Online (Sandbox Code Playgroud)

正确的方法是在实例化时装饰:

class Foo(object):

class Foo(object):

    def __init__(self, args):
        self.method = mem.cache(self.method)

    def method(self, ...):
        pass
Run Code Online (Sandbox Code Playgroud)

  • 人们应该阅读该文档中的警告,如果修改了“self”的_any_属性,则缓存将失效并重新调用方法,建议添加“ignore=['self']”,并且似乎可以为我解决这个问题...... (7认同)