如何pickle Python类方法类型(即用于多处理)?
如何pickle Python类方法类型?它没有在标准库中实现。我发现我可以使用一些第三方模块,比如dill,但是如何只使用pickle 我准备了一些测试代码来简化这个问题:如何pickle Python类方法类型(即用于多处理)?,python,python-2.7,python-multiprocessing,pickle,Python,Python 2.7,Python Multiprocessing,Pickle,如何pickle Python类方法类型?它没有在标准库中实现。我发现我可以使用一些第三方模块,比如dill,但是如何只使用pickle 我准备了一些测试代码来简化这个问题: import pickle class Thing(object): @staticmethod def static_method(): print Thing.static_method.__name__ @classmethod def class_method(cls): pri
import pickle
class Thing(object):
@staticmethod
def static_method():
print Thing.static_method.__name__
@classmethod
def class_method(cls):
print cls.class_method.__name__
def object_method(self):
print self.object_method.__name__
def main():
items = [Thing,
Thing(),
Thing.static_method,
Thing.class_method,
Thing.object_method,
Thing().object_method]
for i, item in enumerate(items):
try:
pickle.loads(pickle.dumps(item))
except Exception as ex:
print i, ex, item
if __name__ == '__main__':
main()
如何处理Python2.7中方法默认缺乏pickle支持的问题
2 Can't pickle <function static_method at 0x025614F0>: it's not found as __main__.static_method <function static_method at 0x025614F0>
3 can't pickle instancemethod objects <bound method type.class_method of <class '__main__.Thing'>>
4 can't pickle instancemethod objects <unbound method Thing.object_method>
5 can't pickle instancemethod objects <bound method Thing.object_method of <__main__.Thing object at 0x024F71D0>>
你见过这个非常相似的问题吗?:我是莳萝的作者。当dill可以对你的所有项目进行pickle时,为什么还要坚持使用pickle?如果是因为你想使用多重处理,那么你可以使用我的多重处理分支paths.multiprocessing。它使用dill序列化程序,因此您可以在map函数中发送任何项。例如请看:@MikeMcKerns我觉得dill太大了,无法解决这个问题加载成本+内存浪费-我可以从dill中选择一些代码并使用,但不想加载所有代码,因为只需要一些函数-我认为这将是100%的1%。@PaintedCone我阅读了答案,但没有发现有用-我找到了一些更好的答案,但没有解决问题@静态法。