Python:无法腌制模块对象错误

adu*_*dum 10 python pickle

我正在尝试挑选一个大班并获得"TypeError:无法挑选模块对象".尽管环顾网络,但我无法弄清楚这意味着什么.我不确定哪个"模块对象"造成了麻烦.有没有办法找到罪魁祸首?堆栈跟踪似乎没有任何表示.

Jos*_*eak 10

递归查找泡菜失败

受到wump评论的启发: Python: can't pickle module objects error

这是一些帮助我递归找到罪魁祸首的快速代码。

它检查有问题的对象,看它是否无法酸洗。

然后迭代尝试 pickle 键以__dict__返回仅失败的 picklings列表。

代码片段

import pickle

def pickle_trick(obj, max_depth=10):
    output = {}

    if max_depth <= 0:
        return output

    try:
        pickle.dumps(obj)
    except (pickle.PicklingError, TypeError) as e:
        failing_children = []

        if hasattr(obj, "__dict__"):
            for k, v in obj.__dict__.items():
                result = pickle_trick(v, max_depth=max_depth - 1)
                if result:
                    failing_children.append(result)

        output = {
            "fail": obj, 
            "err": e, 
            "depth": max_depth, 
            "failing_children": failing_children
        }

    return output

Run Code Online (Sandbox Code Playgroud)

示例程序

import redis

import pickle
from pprint import pformat as pf


def pickle_trick(obj, max_depth=10):
    output = {}

    if max_depth <= 0:
        return output

    try:
        pickle.dumps(obj)
    except (pickle.PicklingError, TypeError) as e:
        failing_children = []

        if hasattr(obj, "__dict__"):
            for k, v in obj.__dict__.items():
                result = pickle_trick(v, max_depth=max_depth - 1)
                if result:
                    failing_children.append(result)

        output = {
            "fail": obj, 
            "err": e, 
            "depth": max_depth, 
            "failing_children": failing_children
        }

    return output


if __name__ == "__main__":
    r = redis.Redis()
    print(pf(pickle_trick(r)))

Run Code Online (Sandbox Code Playgroud)

示例输出

$ python3 pickle-trick.py
{'depth': 10,
 'err': TypeError("can't pickle _thread.lock objects"),
 'fail': Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>,
 'failing_children': [{'depth': 9,
                       'err': TypeError("can't pickle _thread.lock objects"),
                       'fail': ConnectionPool<Connection<host=localhost,port=6379,db=0>>,
                       'failing_children': [{'depth': 8,
                                             'err': TypeError("can't pickle _thread.lock objects"),
                                             'fail': <unlocked _thread.lock object at 0x10bb58300>,
                                             'failing_children': []},
                                            {'depth': 8,
                                             'err': TypeError("can't pickle _thread.RLock objects"),
                                             'fail': <unlocked _thread.RLock object owner=0 count=0 at 0x10bb58150>,
                                             'failing_children': []}]},
                      {'depth': 9,
                       'err': PicklingError("Can't pickle <function Redis.<lambda> at 0x10c1e8710>: attribute lookup Redis.<lambda> on redis.client failed"),
                       'fail': {'ACL CAT': <function Redis.<lambda> at 0x10c1e89e0>,
                                'ACL DELUSER': <class 'int'>,
0x10c1e8170>,
                                .........
                                'ZSCORE': <function float_or_none at 0x10c1e5d40>},
                       'failing_children': []}]}
Run Code Online (Sandbox Code Playgroud)

根本原因 - Redis 不能pickle _thread.lock

就我而言,创建一个Redis我保存为对象属性的实例破坏了酸洗。

当你创建Redis它的一个实例时,它也会创建一个connection_poolofThreads并且线程锁不能被pickle。

我必须创建和清理Redismultiprocessing.Process它通过酸洗之前。

测试

就我而言,我试图腌制的班级必须能够腌制。所以我添加了一个单元测试来创建类的一个实例并对其进行腌制。这样,如果有人修改了该类,使其无法被腌制,从而破坏了它在多处理(和 pyspark)中使用的能力,我们将检测到该回归并立即知道。

def test_can_pickle():
    # Given
    obj = MyClassThatMustPickle()

    # When / Then
    pkl = pickle.dumps(obj)

    # This test will throw an error if it is no longer pickling correctly

Run Code Online (Sandbox Code Playgroud)


unu*_*tbu 9

我可以这样重现错误消息:

import cPickle

class Foo(object):
    def __init__(self):
        self.mod=cPickle

foo=Foo()
with file('/tmp/test.out', 'w') as f:
    cPickle.dump(foo, f) 

# TypeError: can't pickle module objects
Run Code Online (Sandbox Code Playgroud)

你有引用模块的class属性吗?

  • 想到的唯一事情是递归下降..在对象上做一个dir(...),并尝试分别腌制每个属性.取一个给出错误的那个,并重复相同直到找到模块对象. (3认同)

Mik*_*rns 9

Python无法挑选模块对象是真正的问题.有充分的理由吗?我不这么认为.让模块对象不可分割会导致python作为并行/异步语言的脆弱性.如果你想腌制模块对象,或几乎任何python中的任何东西,那么使用dill.

Python 3.2.5 (default, May 19 2013, 14:25:55) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> import os
>>> dill.dumps(os)
b'\x80\x03cdill.dill\n_import_module\nq\x00X\x02\x00\x00\x00osq\x01\x85q\x02Rq\x03.'
>>>
>>>
>>> # and for parlor tricks...
>>> class Foo(object):
...   x = 100
...   def __call__(self, f):
...     def bar(y):
...       return f(self.x) + y
...     return bar
... 
>>> @Foo()
... def do_thing(x):
...   return x
... 
>>> do_thing(3)
103 
>>> dill.loads(dill.dumps(do_thing))(3)
103
>>> 
Run Code Online (Sandbox Code Playgroud)

获取dill此:https://github.com/uqfoundation/dill