Remotedienstbibliothek
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Dieses Modul enthält Klassen, die nützlich für die Erstellung von Remotediensten sind, die einem Standardmodell für Requests und Antworten entsprechen. Ein Dienst muss wie die folgende Klasse gestaltet sein, um diesem Modell zu entsprechen:
# Each service instance only handles a single request and is then discarded.
# Make these objects light weight.
class Service(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)
def remote_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.
def initialize_request_state(self, state):
...
Die Klasse Service ist eine praktische Basisklasse mit den oben genannten Funktionen. Sie implementiert alle erforderlichen und optionalen Methoden für einen Dienst. Die Klasse bietet außerdem praktische Methoden zum Erstellen von Factory-Funktionen, die einen nichtflüchtigen globalen Status an eine neue Dienstinstanz weitergeben können.
Mit dem Remote-Decorator wird deklariert, welche Methoden einer Klasse RPCs verarbeiten sollen. Dagegen ist dieser Decorator nicht für die Verarbeitung von Sockets und verschiedener zugrunde liegender RPC-Protokolle zuständig. Der Decorator sorgt dafür, dass Sie den richtigen Anfragetyp verwenden, aber er überprüft nicht die Initialisierung.
Wird der Remote Decorator bei einer Methode verwendet, wird der Wrapper-Methode das Attribut "remote" zugeordnet. Diese Eigenschaft enthält den request_type
und response_type
, die von der Implementierung der Methode erwartet werden.
Der Remote Decorator bietet keine eigenständige Unterstützung, um abgeleitete Klassen für Remotemethoden zu erstellen. Verwenden Sie zur Erweiterung eines Dienstes die Methode für abgeleitete Klassen. Beispiel:
class MyService(Service):
@method(DoSomethingRequest, DoSomethingResponse)
def do_something(self, request):
... implement do-something ...
class MyBetterService(MyService):
@method(DoSomethingRequest, DoSomethingResponse)
def do_something(self, request):
response = super(MyBetterService, self).do_something.remote.method(request)
... do something with response ...
return response
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-09-04 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eThis module facilitates building remote services that adhere to a standard request and response model, requiring each service to be structured like the provided \u003ccode\u003eService\u003c/code\u003e class example.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eService\u003c/code\u003e class serves as a base, providing essential and optional methods, and includes convenience methods for creating factory functions to manage persistent global state.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eremote\u003c/code\u003e decorator is used to designate RPC-serving methods within a class, ensuring the correct request and response types are used, though it doesn't handle initialization or underlying protocols.\u003c/p\u003e\n"],["\u003cp\u003eMethods wrapped with the \u003ccode\u003eremote\u003c/code\u003e decorator gain a \u003ccode\u003eremote\u003c/code\u003e property, which holds the \u003ccode\u003erequest_type\u003c/code\u003e and \u003ccode\u003eresponse_type\u003c/code\u003e information expected by the method's implementation.\u003c/p\u003e\n"],["\u003cp\u003eExtending service methods involves subclassing and redecorating with the \u003ccode\u003emethod\u003c/code\u003e decorator to ensure proper type handling and to call the super class.\u003c/p\u003e\n"]]],[],null,["# The Remote Service Library\n\nThis module contains classes that are useful for building remote services that conform to a standard request and response model. To conform to this model,\na service must be like the following class: \n\n```python\n# Each service instance only handles a single request and is then discarded.\n# Make these objects light weight.\nclass Service(object):\n\n # It must be possible to construct service objects without any parameters.\n # If your constructor needs extra information you should provide a\n # no-argument factory function to create service instances.\n def __init__(self):\n ...\n\n # Each remote method must use the 'remote' decorator, passing the request\n # and response message types. The remote method itself must take a single\n # parameter which is an instance of RequestMessage and return an instance\n # of ResponseMessage.\n @method(RequestMessage, ResponseMessage)\n def remote_method(self, request):\n # Return an instance of ResponseMessage.\n\n # A service object may optionally implement a 'initialize_request_state'\n # method that takes as a parameter a single instance of a RequestState. If\n # a service does not implement this method it will not receive the request\n # state.\n def initialize_request_state(self, state):\n ...\n```\n\nThe [Service](/appengine/docs/legacy/standard/python/tools/protorpc/remote/serviceclass) 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.\n\nThe 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.\n\nWhen the remote decorator is used on a method, the wrapper method will have a\n'remote' property associated with it. This property contains the\n`request_type` and `response_type` expected by the method's implementation.\n\nOn its own, the remote decorator does not provide any support for subclassing\nremote methods. In order to extend a service, use the subclass methods to redecorate. For example: \n\n```python\nclass MyService(Service):\n\n @method(DoSomethingRequest, DoSomethingResponse)\n def do_something(self, request):\n ... implement do-something ...\n\nclass MyBetterService(MyService):\n\n @method(DoSomethingRequest, DoSomethingResponse)\n def do_something(self, request):\n response = super(MyBetterService, self).do_something.remote.method(request)\n ... do something with response ...\n return response\n```"]]