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 Service Class
Stay organized with collections
Save and categorize content based on your preferences.
Google Protocol RPC services are constructed either via a constructor or a factory which takes no parameters. However, some applications need to pass some state or configuration into a service across multiple requests. To do this, define parameters to the constructor of the service and use the new_factory() class method to build a factory that will transmit parameters to the constructor. For example:
from protorpc import remote
class MyService(remote.Service):
def __init__(self, configuration, state):
self.configuration = configuration
self.state = state
configuration = MyServiceConfiguration()
global_state = MyServiceState()
my_service_factory = MyService.new_factory(configuration,
state=global_state)
The contract with any service handler is that a new service object is created to handle each user request, and that the construction does not take any parameters. The factory satisfies this condition:
new_instance = my_service_factory()
assert new_instance.state is global_state
Service
is provided by the protorpc.remote
module.
Class Properties
Services instances one property:
- request_state
- Request state associated with this Service instance.
Class Methods
The Service class provides the following class methods:
- all_remote_methods()
-
Gets all remote methods for a Service class.
Note: Built-in methods do not appear in the dictionary of remote methods.
Returns a dictionary that maps method names to remote methods.
- new_factory(args, **kwargs)
-
Creates a factory for a service. Useful for passing configuration or state objects to the service. Accepts arbitrary parameters and keywords, however, underlying service must accept also accept not other parameters in its constructor.
Arguments
- args
- Arguments to pass to the service constructor.
- **kwargs
Returns a factory function that creates a new instance and forwards arguments and keywords to the constructor.
Instance Methods
Service instances have the following methods:
- initialize_request_state(request_state)
- Arguments:
- request_state
- A RequestState instance.
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-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003eGoogle Protocol RPC services typically use parameterless constructors or factories, but the \u003ccode\u003enew_factory()\u003c/code\u003e method allows for passing state or configuration to the service constructor.\u003c/p\u003e\n"],["\u003cp\u003eA new service object is created for each user request, adhering to the contract that service construction does not take parameters, a condition fulfilled by the factory.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eService\u003c/code\u003e class from \u003ccode\u003eprotorpc.remote\u003c/code\u003e offers class methods like \u003ccode\u003eall_remote_methods()\u003c/code\u003e to get all remote methods, excluding built-in methods, and \u003ccode\u003enew_factory()\u003c/code\u003e to pass configuration or state objects during service creation.\u003c/p\u003e\n"],["\u003cp\u003eService instances have one property, \u003ccode\u003erequest_state\u003c/code\u003e, and one instance method, \u003ccode\u003einitialize_request_state(request_state)\u003c/code\u003e which initializes a RequestState instance.\u003c/p\u003e\n"]]],[],null,[]]