python - 如何在python任务的有效负载中发送类对象?我想在任务的参数中发送一个对象。
当我使用simplejson
时,我得到错误:Object is not serializable
。
当我使用泡菜时,我得到KeyValue Error
.
这该怎么做 ?
这是我要序列化的类
class Matrix2D_icfg:
name = ""
indices = []
value = {}
def __init__(self,s):
self.name = s
self.indices = []
def __getitem__(self,i):
self.indices.append(i)
if len(self.indices)==2:
(m,n) = self.indices
self.indices = []
if self.value.has_key(m*4276+n) == True :
value = self.value[m*4276+n]
else :
value = 0
return value
else: return self
def __setitem__(self,i,value):
self.indices.append(i)
if len(self.indices)==2:
(m,n) = self.indices
if value != 0 : self.value[m*4276+n] = value
self.indices = []
return self
icfg = Matrix2D_icfg("icfg") #declaring object
icfg_compress = pickle.dumps(icfg) #to pickle
icfg = pickle.loads(icfg_compress) # to unload
当我将腌制对象作为有效负载传递并稍后将其卸载时,出现以下错误
File "/Users/praveensekar/myFYP/gaecode/pknots4d.2.3/pknots.py", line 439, in post
icfg = pickle.loads(icfg_compress)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pickle.py", line 1374, in loads
return Unpickler(file).load()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/pickle.py", line 858, in load
dispatch[key](self)
KeyError: '\x00'