How to repeat urlfetch.fetch several times in case of error?

Quite often, GAE cannot load the file, and I get the following error:

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 to retry in the event of such an 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?
+5
source share
1 answer

If you can, move this work to the task queue . When tasks fail, they are automatically repeated. If they continue to fail, the system gradually discards the repetition rate as slowly as once an hour. This is an easy way to process API requests for speed-limited services without implementing a single retry logic.

, - :

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, .

+9

All Articles