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

标签 python google-app-engine exception-handling

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/

相关文章:

node.js - 如何使用 NodeJs 实现 Google App 引擎冗余

wcf - FaultException.Detail返回为空

python - 如何用conda安装最新的开发版Python?

python - 计算丢失的比特币私钥的校验和比特币

google-app-engine - 如何获取 Google App Engine 上的命名空间列表?

java - App Engine 响应时间慢及优化

c# - 永不捕获的异常

安卓异常处理

python - 如何从 jsonline 文件中的每一行提取元素?

Python从文件中提取部分信息