Python 2.7 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Python 2.7 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Python 2.7 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Python 版本。
RetryParams 类
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
RetryParams
由位于 src/cloudstorage
中适用于 Cloud Storage 的 App Engine 客户端库提供。此类可让您更改用于处理超时和重试的默认设置。
简介
在尝试联系 Google Cloud Storage 服务器的过程中,适用于 Cloud Storage 的 App Engine 客户端库会使用默认设置来处理超时和重试。
此类可让您在应用范围内更改这些设置,或者针对库函数(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 秒的任意值。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]