Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
RetryParams 類別
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
RetryParams
由 src/cloudstorage
中,Cloud Storage 適用的 App Engine 用戶端程式庫提供。這個類別可讓您變更用於處理逾時情形和重試作業的預設設定。
簡介
Cloud Storage 適用的 App Engine 用戶端程式庫使用預設設定,以處理嘗試與 Google Cloud Storage 伺服器連線時的逾時和重試作業。這個類別可讓您針對整個應用程式變更這些設定,或是針對程式庫函式 (copy2
、delete
、listbucket
、open
、stat
) 的特定叫用變更設定。您只需變更所需的特定設定,因為除非明確覆寫,否則所有其他設定都會保留。每個 RetryParams
例項都是每個執行緒和每個要求的專屬例項。
如要變更應用程式全域的預設設定,請建立 RetryParams
物件,指定要變更的任何設定,然後將此物件提供給 cloudstorage.set_default_retry_params()
函式,如下所示:
my_default_retry_params = cloudstorage.RetryParams(initial_delay=0.2,
max_delay=5.0,
backoff_factor=2,
max_retry_period=15)
cloudstorage.set_default_retry_params(my_default_retry_params)
如要只變更特定函式呼叫的預設設定,請建立 RetryParams
物件,然後直接將其提供至函式中的 retry_params
參數,如下所示:
write_retry_params = cloudstorage.RetryParams(backoff_factor=1.1)
gcs_file = cloudstorage.open(filename,
'w',
content_type='text/plain',
options={'x-goog-meta-foo': 'foo',
'x-goog-meta-bar': 'bar'},
retry_params=write_retry_params)
這對應用程式或其他函式叫用使用的預設設定不會造成任何影響。
執行個體屬性
RetryParams
例項具有下列屬性:
- initial_delay
- 重試前要延遲的秒數。預設值為
0.1
。延遲有助於分散 Google Cloud Storage 伺服器的負載。 - backoff_factor
- 指數輪詢倍數,用來決定最佳處理速率。預設值為
2.0
。如需設定這個值的說明和建議,請參閱有關輪詢的 Google Cloud Storage 說明文件。 - max_delay
- 重試之間的等待秒數上限。預設值為
10.0
。
- min_retries
- 重試次數下限。預設值為
3
。
- max_retries
- 重試次數上限。如果您完全不想重試,請將這個值設為 0。預設值為
6
。
- max_retry_period
- 特定要求所有重試行為的秒數上限。預設值為
30.0
。如果已經過這段時間且重試了 min_retries
次,就會停止重試。 - urlfetch_timeout
- 傳回逾時錯誤前,等待 UrlFetch 與 Google Cloud Storage 伺服器通訊的秒數。預設值設為
None
,表示使用預設 UrlFetch 期限 (即 5 秒)。您可以將這個項目設為 60 秒以內的任何值。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","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-09-04 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eRetryParams\u003c/code\u003e allows customization of timeout and retry settings for the App Engine client library's interactions with Cloud Storage.\u003c/p\u003e\n"],["\u003cp\u003eYou can adjust settings such as \u003ccode\u003einitial_delay\u003c/code\u003e, \u003ccode\u003ebackoff_factor\u003c/code\u003e, \u003ccode\u003emax_delay\u003c/code\u003e, \u003ccode\u003emin_retries\u003c/code\u003e, \u003ccode\u003emax_retries\u003c/code\u003e, \u003ccode\u003emax_retry_period\u003c/code\u003e, and \u003ccode\u003eurlfetch_timeout\u003c/code\u003e to control retry behavior.\u003c/p\u003e\n"],["\u003cp\u003eSettings can be changed application-wide using \u003ccode\u003ecloudstorage.set_default_retry_params()\u003c/code\u003e or for specific function calls by providing \u003ccode\u003eretry_params\u003c/code\u003e to the function directly.\u003c/p\u003e\n"],["\u003cp\u003eEach \u003ccode\u003eRetryParams\u003c/code\u003e instance is specific to a thread and request, allowing for granular control over retries without affecting other parts of the application.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eurlfetch_timeout\u003c/code\u003e setting lets users determine how many seconds to wait for UrlFetch to contact the Cloud Storage servers, defaulting to 5 seconds, but going as high as 60 seconds.\u003c/p\u003e\n"]]],[],null,["# The RetryParams Class\n\n`RetryParams` is provided by the App Engine client library for Cloud Storage contained\nin `src/cloudstorage`. This class lets you change default settings used to\nhandle timeouts and retries.\n\nIntroduction\n------------\n\nThe App Engine client library for Cloud Storage uses default settings for handling\ntimeouts and retries during attempts to contact Google Cloud Storage servers.\nThis class allows you to change those settings, either on an application-wide\nbasis, or for a specific invocation of a library\nfunction (`copy2`, `delete`, `listbucket`, `open`, `stat`). You need change only\nthe specific setting you are interested in, as all other settings are retained\nunless explicitly overwritten. Each `RetryParams` instance is unique per thread\nand per request.\n\nTo change a default setting application-wide, you create a `RetryParams`\nobject, specify whatever settings you wish to change, and supply this object to\nthe `cloudstorage.set_default_retry_params()` function as follows: \n\n my_default_retry_params = cloudstorage.RetryParams(initial_delay=0.2,\n max_delay=5.0,\n backoff_factor=2,\n max_retry_period=15)\n cloudstorage.set_default_retry_params(my_default_retry_params)\n\nTo change default settings for a specific function invocation only, create\na `RetryParams` object and supply it directly to the function in its\n`retry_params` parameter, as follows: \n\n write_retry_params = cloudstorage.RetryParams(backoff_factor=1.1)\n gcs_file = cloudstorage.open(filename,\n 'w',\n content_type='text/plain',\n options={'x-goog-meta-foo': 'foo',\n 'x-goog-meta-bar': 'bar'},\n retry_params=write_retry_params)\n\nThis has no effect on the default settings used by the app or by other\nfunction invocations.\n\nInstance properties\n-------------------\n\nA `RetryParams` instance has the following properties:\n\ninitial_delay\n: The number of seconds to delay before a retry. Default is `0.1`. Delay helps\n distribute load at the Google Cloud Storage server.\n\nbackoff_factor\n: The exponential back-off multiplier, used to determine optimal processing\n rate. Default is `2.0`. For a description and recommendations for setting this\n value, see the Google Cloud Storage documentation on [backoff](/storage/docs/exponential-backoff).\n\nmax_delay\n: The maximum number of seconds to wait between retries. Default is `10.0`.\n\nmin_retries\n: The minimum number of times to retry. Default is `3`.\n\nmax_retries\n: The maximum number of times to retry. Set this value to 0 if\n you don't want any retries. Default is `6`.\n\nmax_retry_period\n: The maximum number of seconds that can be spent on all retries of a\n given request. Default is `30.0`. Retry stops when this period passed AND\n `min_retries` has been attempted.\n\nurlfetch_timeout\n: The number of seconds to wait for UrlFetch to contact the\n Google Cloud Storage servers before returning a timeout error. By default,\n this is set to `None`, which means use the [default UrlFetch deadline](/appengine/docs/legacy/standard/python/issue-requests),\n which is five seconds. You can set this to any value up to a maximum of 60\n seconds."]]