Google App Engine有效内容对象 [英] Google App Engine Payload Object
本文介绍了Google App Engine有效内容对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在python中的任务负载中发送类对象?我想在任务的参数中发送一个对象。
当我使用 simplejson
时,出现错误: Object不可序列化
。
当我使用pickle时,得到 KeyValue Error
。
如何做这个 ?
How to send a class object in the payload of a task in python? I want to send an object in the parameters of a task.
When I use simplejson
, I get the error: Object is not serializable
.
When I use pickle, I get KeyValue Error
.
How to do this ?
这是我要序列化的类
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
当我将pickle对象作为有效载荷并将其卸载时,出现以下错误稍后
I get the following error when i pass the pickled object as payload and unload it later
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'
推荐答案
问题出在卸载的数据类型上。我铸造它键入str和一切似乎正常工作。
我只是将它改为
The problem was with the type of data that was unloaded. I casted it to type str and everything seemed to work properly. I just changed it to
icfg = Matrix2D_icfg("icfg") #declaring object
icfg_compress = pickle.dumps(icfg) #to pickle
icfg = pickle.loads(str(icfg_compress)) # to unload
这篇关于Google App Engine有效内容对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文