How to use Memcache with ProtoRPC (Google App Engine - Python)

I am trying to use memcache with ProtoRPC to speed up the process (Google App Engine with Python). To simplify the question, I used the Hello World example and modified it a bit. Here is my version:

from protorpc import messages from protorpc import remote from protorpc.wsgi import service from google.appengine.api import memcache class HelloRequest(messages.Message): my_name = messages.StringField(1, required=True) class ElementOfArrayResponse(messages.Message): value=messages.IntegerField(1) class HelloResponse(messages.Message): hello = messages.StringField(1, required=True) list = messages.MessageField(ElementOfArrayResponse, 2, repeated=True) class HelloService(remote.Service): @remote.method(HelloRequest, HelloResponse) def hello(self, request): response = memcache.get(request.my_name) if response is None: list = [] for i in range(7): list.append(SomeElementResponse(value=i)) response = HelloResponse(hello='Hello there, %s!' % request.my_name, list=list) memcache.set(request.my_name,response) return response app = service.service_mappings([('/hello.*', HelloService)]) 

But, unfortunately, this code returns an error that: ERROR 2013-01-12 17:17:31,081 service.py:196] Encountered unexpected error from ProtoRPC method implementation: PicklingError (Can't pickle <type 'weakref'>: attribute lookup __builtin__.weakref failed) .

However, when I use almost the same code with the original HelloResponse , memcache works fine.

So what am I doing wrong?

+4
source share
1 answer

two things, it looks like your memcache key is a List object (want you to know about this). next to your code you will have to serialize the message object when installing / exiting memcache api.

rewrite here:

 from protorpc import remote from protorpc import messages from protorpc import protojson from protorpc.wsgi import service from google.appengine.api import memcache class HelloRequest(messages.Message): my_name = messages.StringField(1, required=True) class ElementOfArrayResponse(messages.Message): value=messages.IntegerField(1) class HelloResponse(messages.Message): hello = messages.StringField(1, required=True) list = messages.MessageField(ElementOfArrayResponse, 2, repeated=True) class HelloService(remote.Service): @remote.method(HelloRequest, HelloResponse) def hello(self, request): response = memcache.get(request.my_name) if response: # decode the value to a message.. response = protojson.decode_message(HelloResponse, response) else: list = [] for i in range(7): list.append(SomeElementResponse(value=i)) response = HelloResponse(hello='Hello there, %s!' % request.my_name, list=list) # encode the message to a serializable format.. value = protojson.encode_message(message) memcache.set(request.my_name, value) return response app = service.service_mappings([('/hello.*', HelloService)]) 
+5
source

All Articles