Python 2.7 ha raggiunto la fine del supporto
e verrà
ritirato
il 31 gennaio 2026. Dopo il ritiro, non potrai eseguire il deployment di applicazioni Python 2.7, anche se la tua organizzazione ha utilizzato in precedenza un criterio dell'organizzazione per riattivare i deployment di runtime legacy. Le tue applicazioni Python 2.7 esistenti continueranno a essere eseguite e a ricevere traffico dopo la
data di ritiro. Ti consigliamo di
eseguire la migrazione all'ultima versione supportata di Python.
La classe di servizio
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
I servizi RPC di protocollo Google vengono costruiti tramite un costruttore o una factory che non accetta parametri. Tuttavia, alcune applicazioni devono trasmettere uno stato o una configurazione a un servizio tramite più richieste. Per farlo, definisci i parametri per il costruttore del servizio e utilizza il metodo di classe new_factory() per creare una factory che trasmetta i parametri al costruttore. Ad esempio:
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)
Il contratto con qualsiasi gestore di servizi prevede che venga creato un nuovo oggetto di servizio per gestire ogni richiesta dell'utente e che la costruzione non accetti parametri. La fabbrica soddisfa questa condizione:
new_instance = my_service_factory()
assert new_instance.state is global_state
Service
è fornito dal modulo protorpc.remote
.
Proprietà del corso
Le istanze di servizi di una proprietà:
- request_state
- Stato della richiesta associato a questa istanza di servizio.
Metodi di classe
La classe Service fornisce i seguenti metodi di classe:
- all_remote_methods()
-
Recupera tutti i metodi remoti per una classe Service.
Nota:i metodi integrati non vengono visualizzati nel dizionario dei metodi remoti.
Restituisce un dizionario che mappa i nomi dei metodi ai metodi remoti.
- new_factory(args, **kwargs)
-
Crea una factory per un servizio. Utile per passare oggetti di configurazione o stato al servizio. Accetta parametri e parole chiave arbitrari, tuttavia il servizio sottostante deve accettare anche altri parametri nel relativo costruttore.
Argomenti
- args
- Argomenti da passare al costruttore del servizio.
- **kwargs
Restituisce una funzione di fabbrica che crea una nuova istanza e inoltra gli argomenti e le parole chiave al costruttore.
Metodi istanza
Le istanze di servizio hanno i seguenti metodi:
- initialize_request_state(request_state)
- Argomenti:
- request_state
- Un'istanza di RequestState.
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-09-04 UTC.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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."]]