Skip to content

Commit 1ef2327

Browse files
authored
Fix retry error in download when exception occurs (#32816)
* fix retry in download when exception occurs
1 parent 84eca16 commit 1ef2327

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

python/paddle/tests/test_download.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ def test_get_path_from_url(self):
7070
for url in urls:
7171
get_path_from_url(url, root_dir='./test')
7272

73+
def test_retry_exception(self, ):
74+
with self.assertRaises(RuntimeError):
75+
from paddle.utils.download import _download
76+
_download(
77+
'www.baidu.com',
78+
'./test', )
79+
7380

7481
if __name__ == '__main__':
7582
unittest.main()

python/paddle/utils/download.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,15 @@ def _download(url, path, md5sum=None):
186186

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

189-
req = requests.get(url, stream=True)
189+
try:
190+
req = requests.get(url, stream=True)
191+
except Exception as e: # requests.exceptions.ConnectionError
192+
logger.info(
193+
"Downloading {} from {} failed {} times with exception {}".
194+
format(fname, url, retry_cnt + 1, str(e)))
195+
time.sleep(1)
196+
continue
197+
190198
if req.status_code != 200:
191199
raise RuntimeError("Downloading from {} failed with code "
192200
"{}!".format(url, req.status_code))

0 commit comments

Comments
 (0)