From 1ef2327944a85c89948496f87c7c92a3ac5af1b2 Mon Sep 17 00:00:00 2001 From: Wenyu Date: Tue, 11 May 2021 19:39:11 +0800 Subject: [PATCH] Fix retry error in download when exception occurs (#32816) * fix retry in download when exception occurs --- 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