From 9035fd2e5564b57010b67e26b5f8153ab7cf77e6 Mon Sep 17 00:00:00 2001 From: Wenyu Date: Thu, 10 Jun 2021 16:52:53 +0800 Subject: [PATCH] [cherry-pick] Fix retry error in download when exception occurs #32816 (#33454) * fix retry in download when exception occurs * add test_retry_exception --- python/paddle/tests/test_download.py | 7 +++++++ python/paddle/utils/download.py | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/python/paddle/tests/test_download.py b/python/paddle/tests/test_download.py index b8af7f6a80e..4be2dde1bcc 100644 --- a/python/paddle/tests/test_download.py +++ b/python/paddle/tests/test_download.py @@ -70,6 +70,13 @@ class TestDownload(unittest.TestCase): 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() diff --git a/python/paddle/utils/download.py b/python/paddle/utils/download.py index dda8abeff21..3ad627ddea9 100644 --- a/python/paddle/utils/download.py +++ b/python/paddle/utils/download.py @@ -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)) -- GitLab