简体   繁体   English

Google App Engine有效负载对象

[英]Google App Engine Payload Object

How to send a class object in the payload of a task in python? 如何在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 . 当我使用simplejson ,出现错误: Object is not serializable
When I use pickle, I get KeyValue Error . 使用泡菜时,出现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'  

The problem was with the type of data that was unloaded. 问题在于卸载的数据类型。 I casted it to type str and everything seemed to work properly. 我将其强制转换为str类型,一切似乎都正常运行。 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

Have you looked at the deferred library ? 您看过延期图书馆吗? It's designed for exactly this, and takes care of serialization and deserialization for you. 正是为此目的而设计的,并为您处理序列化和反序列化。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM