Google App Engine Payload Object

余生颓废 提交于 2020-01-15 08:01:12

问题


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 ?

This is the class which I want to serialize

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

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'  

回答1:


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



回答2:


Have you looked at the deferred library? It's designed for exactly this, and takes care of serialization and deserialization for you.




回答3:


This is a part of my task queueing service. It just posts a list to another task to break the project up into manageable parts. It's just part of it but you should get most of the idea for what you need to do.

To save it:

from django.utils import simplejson as json

.... stuff happens

index = 0
            current_list = []
            while index < len(item_list):
                if index+500 < len(item_list):
                    for item in item_list[index:index+500]:
                        current_list.append(item.item_number)
                    jsondump = json.dumps(current_list)
                    taskqueue.add(url = '/queuer', 
                                  headers = {'Content-Type':'application/json'},
                                  payload = jsondump)

To load it:

from django.utils import simplejson as json

    class TaskQueuer(webapp.RequestHandler):
    def post(self):
        request = self.request.body
        task_list = json.loads(request)


来源:https://stackoverflow.com/questions/4836697/google-app-engine-payload-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!