gpt4 book ai didi

python - 如果出错,如何重试 urlfetch.fetch 几次?

转载 作者:太空狗 更新时间:2023-10-30 00:46:42 24 4
gpt4 key购买 nike

GAE 经常无法上传文件,我收到以下错误:

ApplicationError: 2
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 636, in __call__
handler.post(*groups)
File "/base/data/home/apps/picasa2vkontakte/1.348093606241250361/picasa2vkontakte.py", line 109, in post
headers=headers
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 260, in fetch
return rpc.get_result()
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 355, in _get_fetch_result
raise DownloadError(str(err))
DownloadError: ApplicationError: 2

如果出现此类错误,我应该如何进行重试?

        try:
result = urlfetch.fetch(url=self.request.get('upload_url'),
payload=''.join(data),
method=urlfetch.POST,
headers=headers
)
except DownloadError:
# how to retry 2 more times?
# and how to verify result here?

最佳答案

如果可以,请将此作品移至the task queue .当任务失败时,它们会自动重试。如果它们继续失败,系统会逐渐将重试频率降低到每小时一次。这是一种无需实现一次性重试逻辑即可处理对限速服务的 API 请求的简单方法。

如果你真的需要同步处理请求,像这样的东西应该可以工作:

for i in range(3):
try:
result = urlfetch.fetch(...)
# run success conditions here
break
except DownloadError:
#logging.debug("urlfetch failed!")
pass

您还可以将 deadline=10 传递给 urlfetch.fetch 以将默认超时期限加倍。

关于python - 如果出错,如何重试 urlfetch.fetch 几次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5883116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com