Klasse "Service"
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Google Protocol RPC-Dienste werden entweder über einen Konstruktor oder über eine Factory erstellt, die keine Parameter akzeptiert. Einige Anwendungen müssen jedoch über mehrere Anforderungen einen bestimmten Zustand oder eine bestimmte Konfiguration an einen Dienst übergeben. Definieren Sie dazu Parameter für den Konstruktor des Dienstes und verwenden Sie die Klassenmethode new_factory(), um eine Factory zu erstellen, die Parameter an den Konstruktor überträgt. Beispiel:
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)
Die Übereinkunft mit einem Dienst-Handler sieht vor, dass für die Verarbeitung jeder Nutzeranforderung ein neues Dienstobjekt erstellt wird und dass bei der Erstellung keine Parameter verwendet werden. Die Factory erfüllt diese Bedingung:
new_instance = my_service_factory()
assert new_instance.state is global_state
Service
wird vom Modul protorpc.remote
bereitgestellt.
Klasseneigenschaften
Dienstinstanzen weisen eine einzige Eigenschaft auf:
- request_state
- Der mit dieser Dienstinstanz verknüpfte Anforderungszustand.
Klassenmethoden
Die Dienstklasse bietet folgende Klassenmethoden:
- all_remote_methods()
-
Ruft alle Remote-Methoden für eine Dienstklasse ab.
Hinweis: Integrierte Methoden werden nicht im Wörterbuch der Remotemethoden aufgeführt.
Gibt ein Wörterbuch zurück, das Methodennamen Remotemethoden zuordnet.
- new_factory(args, **kwargs)
-
Erstellt eine Factory für einen Dienst. Nützlich zur Weitergabe von Konfigurations- bzw. Zustandsobjekten an den Dienst. Nimmt beliebige Parameter und Keywords an, der zugrunde liegende Dienst darf jedoch keine weiteren Parameter in seinem Konstruktor annehmen.
Argumente
- args
- An den Dienstkonstruktor weiterzugebende Argumente:
- **kwargs
Gibt eine Factory-Funktion zurück, die eine neue Instanz erstellt und Argumente und Keywords an den Konstruktor weiterleitet.
Instanzmethoden
Dienstinstanzen weisen die folgenden Methoden auf:
- initialize_request_state(request_state)
- Argumente:
- request_state
- Eine RequestState-Instanz.
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\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,["# The Service Class\n\nGoogle 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()](#new_factory) class method to build a factory that will transmit parameters to the constructor. For example: \n\n from protorpc import remote\n\n\n class MyService(remote.Service):\n\n def __init__(self, configuration, state):\n self.configuration = configuration\n self.state = state\n\n configuration = MyServiceConfiguration()\n global_state = MyServiceState()\n\n my_service_factory = MyService.new_factory(configuration,\n state=global_state)\n\nThe 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: \n\n new_instance = my_service_factory()\n assert new_instance.state is global_state\n\n`Service` is provided by the `protorpc.remote` module.\n\nClass Properties\n----------------\n\nServices instances one property:\n\nrequest_state\n: Request state associated with this Service instance.\n\nClass Methods\n-------------\n\nThe Service class provides the following class methods:\n\nall_remote_methods()\n\n: Gets all remote methods for a Service class.\n\n **Note:** Built-in methods do not appear in the dictionary of remote methods.\n\n Returns a dictionary that maps method names to remote methods.\n\nnew_factory(args, \\*\\*kwargs)\n\n: 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.\n\n **Arguments**\n\n args\n : Arguments to pass to the service constructor.\n\n \\*\\*kwargs\n\n Returns a factory function that creates a new instance and forwards arguments and keywords to the constructor.\n\nInstance Methods\n----------------\n\nService instances have the following methods:\n\ninitialize_request_state(request_state)\n:\n: Arguments:\n\n request_state\n : A [RequestState](/appengine/docs/legacy/standard/python/tools/protorpc/remote/requeststateclass) instance."]]