服務類別
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Google Protocol RPC 服務是透過建構函式或是無需任何參數的 Factory 來建構。不過,有些應用程式必須將部分狀態或設定傳遞至某個服務,以因應多次要求。為了達成此目的,需將參數定義至該服務的建構函式,並使用 new_factory() 類別方法,建構可將參數傳送到建構函式的 Factory。例如:
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)
與任何服務處理常式定下的約定是,會建立新的服務物件來處理每個使用者要求,至於建構過程則不需要使用任何參數。該 Factory 可滿足以下情況:
new_instance = my_service_factory()
assert new_instance.state is global_state
Service
由 protorpc.remote
模組提供。
類別屬性
服務實例具有一項屬性:
- request_state
- 與此服務例項相關聯的要求狀態。
類別方法
Service 類別提供下列類別方法:
- all_remote_methods()
-
取得 Service 類別的所有遠端方法。
注意:內建方法不會出現在遠端方法的字典中。
傳回字典,將方法名稱對應到遠端方法。
- new_factory(args, **kwargs)
-
建立服務的 Factory,以便於將設定或狀態物件傳遞給服務。可接受任何參數和關鍵字,不過其基礎服務的建構函式中不可接受其他參數。
引數
- args
- 要傳遞至服務建構函式的引數。
- **kwargs
傳回 Factory 函式,該函式會建立新的實例,並將引數和關鍵字轉遞給建構函式。
實例方法
Service 例項的方法如下:
- initialize_request_state(request_state)
- 引數:
- request_state
- RequestState 例項。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-16 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-06-16 (世界標準時間)。"],[[["Google Protocol RPC services typically use parameterless constructors or factories, but the `new_factory()` method allows for passing state or configuration to the service constructor."],["A 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."],["`Service` class from `protorpc.remote` offers class methods like `all_remote_methods()` to get all remote methods, excluding built-in methods, and `new_factory()` to pass configuration or state objects during service creation."],["Service instances have one property, `request_state`, and one instance method, `initialize_request_state(request_state)` which initializes a RequestState instance."]]],[]]