Python用一个类实例来解开一个对象

时间:2014-12-30 20:13:38

标签: python pickle

我正在使用python请求库并试图保持会话。

由于我的主机上有多个IP,因此我创建了以下方法,以便将会话绑定到特定的IP。

class SourceAddressAdapter(HTTPAdapter):
    def __init__(self, source_address, **kwargs):
        self.source_address = source_address
        super(SourceAddressAdapter, self).__init__(**kwargs)

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                   maxsize=maxsize,
                                   block=block,
                                   source_address=self.source_address)

以下代码段用于调用此类:

r = requests.Session()
r.mount('http://', SourceAddressAdapter((self.ip,0)))
r.mount('https://', SourceAddressAdapter((self.ip,0)))

将http和https协议安装到此适配器后,我使用pickle将对象持久化为redis,如下所示:

session = pickle.dumps(r)
redis.hset('sessions',id,session)

当我尝试取消关联会话对象时出现问题:

s=redis.hget('sessions', id)
pickle.loads(s)


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1217, in load_build
    setstate(state)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 114, in __setstate__
    block=self._pool_block)
  File "network_driver.py", line 158, in init_poolmanager
    source_address=self.source_address)
AttributeError: 'SourceAddressAdapter' object has no attribute 'source_address'

它抱怨SourceAddressAdapter没有source_address属性。在我将此类SourceAddressAdapter添加到我的会话之前,序列化运行良好。

所以我猜这是自定义类酸洗/去除斑点的问题。


更新:

在我将__getstate____setstate__方法添加到SourceAddressAdapter后,它可以正常工作

def __getstate__(self):
    # it calls HTTPAdapter's __getstate__()
    state = super(SourceAddressAdapter, self).__getstate__() 
    state['source_address'] = self.source_address
    return state

def __setstate__(self,state):
    self.source_address = state['source_address']
    # Call HTTPAdapter's __setstate__ function to pack the attributes in parent class 
    super(SourceAddressAdapter, self).__setstate__(state)

1 个答案:

答案 0 :(得分:5)

我认为问题是HTTPAdapter类定义了__setstate__方法。在unpickling时调用此函数,并将实例恢复到pickle状态。但是,HTTPAdapter对您的source_address属性一无所知,因此该属性未被恢复(或者甚至可能首先没有被腌制)。

要解决此问题,您需要覆盖__setstate__函数,有点像这样:

def __setstate__(self, state):
    self.source_address= state['source_address'] # do this before calling __setstate__
    HTTPAdapter.__setstate__(self, state)

并且,如前所述,您可能还必须覆盖__getstate__函数,以便source_address被腌制。