Python 2.7 telah mencapai akhir dukungan
dan akan
dihentikan penggunaannya
pada 31 Januari 2026. Setelah penghentian penggunaan, Anda tidak akan dapat men-deploy aplikasi Python 2.7, meskipun organisasi Anda sebelumnya menggunakan kebijakan organisasi untuk mengaktifkan kembali deployment runtime lama. Aplikasi Python 2.7 yang ada akan terus berjalan dan menerima traffic setelah
tanggal penghentiannya. Sebaiknya Anda
bermigrasi ke versi Python terbaru yang didukung.
Class Layanan
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Layanan RPC Google Protocol dibuat melalui konstruktor atau factory yang tidak menggunakan parameter. Namun, beberapa aplikasi harus meneruskan beberapa status atau konfigurasi ke layanan di beberapa permintaan. Untuk melakukannya, tentukan parameter ke konstruktor layanan dan gunakan metode class new_factory() untuk membangun factory yang akan mengirimkan parameter ke konstruktor. Contoh:
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)
Kontrak dengan pengendali layanan adalah objek layanan baru dibuat untuk menangani setiap permintaan pengguna, dan konstruksinya tidak menggunakan parameter apa pun. Factory memenuhi kondisi ini:
new_instance = my_service_factory()
assert new_instance.state is global_state
Service
disediakan oleh modul protorpc.remote
.
Properti Class
Instance layanan satu properti:
- request_state
- Meminta status yang terkait dengan instance Layanan ini.
Metode Class
Class Service menyediakan metode class berikut:
- all_remote_methods()
-
Mendapatkan semua metode jarak jauh untuk class Service.
Catatan: Metode bawaan tidak muncul dalam kamus metode jarak jauh.
Menampilkan kamus yang memetakan nama metode ke metode jarak jauh.
- new_factory(args, **kwargs)
-
Membuat factory untuk layanan. Berguna untuk meneruskan objek konfigurasi atau status ke layanan. Menerima parameter dan kata kunci arbitrer, tetapi layanan dasar juga tidak boleh menerima parameter lain dalam konstruktornya.
Argumen
- args
- Argumen yang akan diteruskan ke konstruktor layanan.
- **kwargs
Menampilkan fungsi factory yang membuat instance baru serta meneruskan argumen dan kata kunci ke konstruktor.
Metode Instance
Instance layanan memiliki metode berikut:
- initialize_request_state(request_state)
- Argumen:
- request_state
- Instance RequestState.
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-09-04 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 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."]]