您的應用程式可使用 Capabilities API 來偵測特定 API 功能的運作中斷和排定停機時間。您可以使用這個 API 來偵測特定功能無法使用的時間,然後略過該功能,藉此縮短應用程式中的停機時間。。
舉例來說,如果您使用 Images API 調整圖片大小,您可以利用 Capabilities API 來偵測 Images API 無法使用的時間,並略過調整大小:
fromgoogle.appengine.apiimportcapabilitiesdefStoreUploadedProfileImage(self):uploaded_image=self.request.get('img')# If the images API is unavailable, we'll just skip the resize.ifcapabilities.CapabilitySet('images').is_enabled():uploaded_image=images.resize(uploaded_image,64,64)store(uploaded_image)
Datastore API 提供了方便資料儲存庫讀取和寫入功能的包裝函式。雖然只需將功能名稱以引數的形式提供給 CapabilitySet(),即可測試功能。但在這種情況下,您也可使用 db.READ_CAPABILITY 和 db.WRITE_CAPABILITY 便利 CapabilitySet 物件。以下範例顯示如何使用便利包裝函式,偵測資料儲存庫的寫入可用性,以及如何在停機期間向使用者提供相關訊息:
fromgoogle.appengine.extimportdbdefRenderHTMLForm(self):ifnotdb.WRITE_CAPABILITY.is_enabled():# Datastore is in read-only mode.
在 Python 2 中使用 Capabilities API
CapabilitySet 類別定義了此 API 所有的可用方法,您可以明確地指出功能,或是透過此類別提供的方法去推測這些功能。請參閱下方內容,取得這個 API 目前已啟用的服務清單。
[[["容易理解","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-05-30 (世界標準時間)。"],[[["The Capabilities API allows applications to detect outages and scheduled downtime for specific API capabilities, helping to reduce application downtime by bypassing unavailable features."],["This API supports first-generation runtimes and can be utilized when upgrading to corresponding second-generation runtimes, with specific migration guidance for the App Engine Python 3 runtime."],["While `is_enabled` generally returns `true`, the \"Datastore writes\" capability returns `false` if Datastore is in read-only mode."],["The API can be used by explicitly naming capabilities or by inferring them from the `CapabilitySet` class methods."],["The API currently supports capabilities like blobstore, Datastore reads and writes, Images, Mail, Memcache, Task Queue, and URL Fetch services."]]],[]]