Skip to content

Commit

Permalink
Fix retry error in download when exception occurs (#32816)
Browse files Browse the repository at this point in the history
* fix retry in download when exception occurs
  • Loading branch information
lyuwenyu authored May 11, 2021
1 parent 84eca16 commit 1ef2327
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions python/paddle/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def test_get_path_from_url(self):
for url in urls:
get_path_from_url(url, root_dir='./test')

def test_retry_exception(self, ):
with self.assertRaises(RuntimeError):
from paddle.utils.download import _download
_download(
'www.baidu.com',
'./test', )


if __name__ == '__main__':
unittest.main()
10 changes: 9 additions & 1 deletion python/paddle/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,15 @@ def _download(url, path, md5sum=None):

logger.info("Downloading {} from {}".format(fname, url))

req = requests.get(url, stream=True)
try:
req = requests.get(url, stream=True)
except Exception as e: # requests.exceptions.ConnectionError
logger.info(
"Downloading {} from {} failed {} times with exception {}".
format(fname, url, retry_cnt + 1, str(e)))
time.sleep(1)
continue

if req.status_code != 200:
raise RuntimeError("Downloading from {} failed with code "
"{}!".format(url, req.status_code))
Expand Down

0 comments on commit 1ef2327

Please sign in to comment.