简体   繁体   English

如果出现错误,如何重试 urlfetch.fetch 几次?

[英]How to retry urlfetch.fetch a few more times in case of error?

Quite often GAE is not able to upload the file and I am getting the following error:很多时候 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

How should I perform retries in case of such error?如果出现此类错误,我应该如何执行重试?

        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?

If you can, move this work into the task queue .如果可以,将这项工作移到任务队列中。 When tasks fail, they retry automatically.当任务失败时,它们会自动重试。 If they continue to fail, the system gradually backs off retry frequency to as slow as once-per hour.如果它们继续失败,系统会逐渐将重试频率降低到每小时一次。 This is an easy way to handle API requests to rate-limited services without implementing one-off retry logic.这是一种处理 API 对限速服务的请求的简单方法,无需实现一次性重试逻辑。

If you really need to handle requests synchronously, something like this should work:如果你真的需要同步处理请求,这样的事情应该可以工作:

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

You can also pass deadline=10 to urlfetch.fetch to double the default timeout deadline.您还可以将deadline=10传递给 urlfetch.fetch 以将默认超时截止时间加倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM