Access current request and store related data

27 views
Skip to first unread message

dro...@yahoo.com

unread,
Apr 5, 2013, 4:51:58 PM4/5/13
to google-prot...@googlegroups.com

In GAE with webapp2, I use webapp2.get_request() and the request registry to store/retrieve request specific information when a user requests a web page. For message requests processed by protorpc, I'm trying to figure out a similar mechanism for storing/retrieving info specific to a request. I know protorpc provides certain request-specific info like request IP by default but I need to add my own data about the request and access it from other parts of my application without passing references with every call. Does anyone know if this is possible?

Rafe Kaplan

unread,
Apr 5, 2013, 5:14:10 PM4/5/13
to google-prot...@googlegroups.com
  ProtoRPC works similarly to webapp and webapp2 in that each request creates a new instance of the request handler.  That means you can store state on the Service subclass instance.

  For example:

    class MyService(remote.Service):

      @remote.method(MyRequest, MyResponse):
      def my_method(self, request):
        self.var1 = 'hello'

  There's also the matter of using WSGI middleware to add specific state information to a request.  I always thought that would be the proper way to do things like handling user sessions and stuff.  ProtoRPC can't do this yet.

  Were you interested in adding this state before the request comes to the remote method?


On Fri, Apr 5, 2013 at 1:51 PM, <dro...@yahoo.com> wrote:

In GAE with webapp2, I use webapp2.get_request() and the request registry to store/retrieve request specific information when a user requests a web page. For message requests processed by protorpc, I'm trying to figure out a similar mechanism for storing/retrieving info specific to a request. I know protorpc provides certain request-specific info like request IP by default but I need to add my own data about the request and access it from other parts of my application without passing references with every call. Does anyone know if this is possible?

--
You received this message because you are subscribed to the Google Groups "ProtoRPC Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-protorpc-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
 - Rafe Kaplan

dro...@yahoo.com

unread,
Apr 5, 2013, 5:45:53 PM4/5/13
to google-prot...@googlegroups.com

Thanks for the quick response. I can add the data after the request gets to the remote method. But is there a way to get that request handler instance similar to the webapp2.get_request() function? I'll be trying to access the request custom state info from other parts of my program and hoping not to pass a reference to that request object everywhere.
To unsubscribe from this group and stop receiving emails from it, send an email to google-protorpc-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
 - Rafe Kaplan

Rafe Kaplan

unread,
Apr 5, 2013, 6:36:03 PM4/5/13
to google-prot...@googlegroups.com
  I see, I definitely misunderstood what you were asking.

  There is not a way to retrieve the request from the global scope.  Even though App Engine isolates requests to individual threads, ProtoRPC is not able to make that assumption because in theory (and I know it's really theoretical at this point :) a server might be running on a fully muti-threaded non-appengine system.

  I recommend that you implement your own and put your request handlers in a decorator.  It ought not to be too tricky using threading.local and a global variable.  You could write a decorator to help you with it:

  _global_state = threadling.local()

  def set_service(method):
    def set_service_wrapper(self, request):
      _global_state.service = self
      return method(self, request)
    return set_service_wrapper

def get_service():
  return _global_state.service



....


class MyService(remote.Service):

  @remote.method(MyRequest, MyResponse)
  @set_service
  def my_method(self, request):
    assert self is get_service()
    



I do like that webapp2 is sensitive to the possibility that a system may not work like App Engine.  Maybe ProtoRPC should do something similar and then have such a method.  Most users will likely be on App Engine and would benefit as you would.



To unsubscribe from this group and stop receiving emails from it, send an email to google-protorpc-d...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
 - Rafe Kaplan
Reply all
Reply to author
Forward
0 new messages