简单方法调用pickling。
methodpickle的Python项目详细描述
MethodPickle(MethodPickle)是一个允许简单酸洗的快速库 以及取消函数和方法调用的勾选。功能与方法模块 加载是自动处理的,方法可以按名称指定为 好。
pickle方法调用的能力允许排队和延迟 执行任意代码这对于并行化、日志记录和 排队等
联系人:
Steve Lacy <github@slacy.com> Twitter: @sklacy http://slacy.com/blog
功能和用法
请参阅test.py中的单元测试以获取更多详细的示例,但是 我将在这里简单介绍一个示例。
from methodpickle.defer import defer # These are the functions that we're going to defer def some_function(x, y): return x*x + y*y # methodpickle supports deferring execution of classmethods as well, so # here's a simple class with a method: def some_class(object): def __init__(self, x): self._x = x def calc(self, y): return (self._x * self._x + y * y) if __name__ == '__main__': # the defer function takes a method and it's arguments, and turns it # into a pickleable object. storable_func = defer(some_function, 5, 4) # So, we pickle that guy into a string. method_str = pickle.dumps(storable_func) # You can now take method_str and do whatever you like with it. Write # it to a database, send it to another process, put it in your logs, # whatever. # Then, you can unpickle the stored method invocation, and run it, # like this: recovered_func = pickle.loads() assert(recovered_func.run() == 5*5 + 4*4) # methodpickle also supports pickling of classmethods. Note that your # class must support pickling and the methods should have no side # effects. i = some_class(2) storable_classmethod = defer(i, 3) classmethod_str = storable_method.dumps() recovered_classmethod = pickle.loads(classmethod_str) assert(recovered_classmethod.run() == 2*2 + 3*3)
为了方便起见,defer函数还有一种decorator形式,称为 推迟。同样,有关更多详细信息,请参见实现或test.py
注意事项
- 函数的所有参数本身都必须是可pickle的。this
- 包括类方法调用的“self”
- 函数和类必须在模块级。内部类和
- 内部函数没有容易发现的导入路径,因此 延迟函数应该位于模块的顶层我会的 建议将它们放在同一个文件中(比如tasks.py)
- 所有方法参数在延迟时都被深度复制。因此,
- 如果将非常大的数据结构传递给延迟方法,则可能 对性能有影响。另外,如果你通过一个可变的 datastructur(dict、list等),随后的修改将具有 无效。
- 注意函数和方法的双重调用两者都是
- 功能和警告。一旦对函数调用进行pickle操作,该值 可能会被解开并且跑不止一次。当心那些 有意想不到的副作用!