Python 2.7 has reached end of support and will be deprecated on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7 applications, even if your organization previously used an organization policy to re-enable deployments of legacy runtimes. Your existing Python 2.7 applications will continue to run and receive traffic after their deprecation date. We recommend that you migrate to the latest supported version of Python.

The Remote Service Library

This module contains classes that are useful for building remote services that conform to a standard request and response model. To conform to this model, a service must be like the following class:

# Each service instance only handles a single request and is then discarded.
# Make these objects light weight.
classService(object):
 # It must be possible to construct service objects without any parameters.
 # If your constructor needs extra information you should provide a
 # no-argument factory function to create service instances.
 def__init__(self):
 ...
 # Each remote method must use the 'remote' decorator, passing the request
 # and response message types. The remote method itself must take a single
 # parameter which is an instance of RequestMessage and return an instance
 # of ResponseMessage.
 @method(RequestMessage, ResponseMessage)
 defremote_method(self, request):
 # Return an instance of ResponseMessage.
 # A service object may optionally implement a 'initialize_request_state'
 # method that takes as a parameter a single instance of a RequestState. If
 # a service does not implement this method it will not receive the request
 # state.
 definitialize_request_state(self, state):
 ...

The Service class is a convenient base class that provides the above functionality. It implements all required and optional methods for a service. It also has convenience methods for creating factory functions that can pass persistent global state to a new service instance.

The remote decorator is used to declare which methods of a class are meant to service RPCs. While this decorator is not responsible for handing sockets and various underlying RPC protocols. The decorator ensures that you are using the correct request type, but it does not check for initialization.

When the remote decorator is used on a method, the wrapper method will have a 'remote' property associated with it. This property contains the request_type and response_type expected by the method's implementation.

On its own, the remote decorator does not provide any support for subclassing remote methods. In order to extend a service, use the subclass methods to redecorate. For example:

classMyService(Service):
 @method(DoSomethingRequest, DoSomethingResponse)
 defdo_something(self, request):
 ... implement do-something ...
classMyBetterService(MyService):
 @method(DoSomethingRequest, DoSomethingResponse)
 defdo_something(self, request):
 response = super(MyBetterService, self).do_something.remote.method(request)
 ... do something with response ...
 return response

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月17日 UTC.