Skip to content

Commit 7009be5

Browse files
authored
feat: ConnectionResetError handling in simple_download (#1586)
1 parent bfd5cd0 commit 7009be5

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

deeppavlov/core/data/utils.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,30 +109,34 @@ def simple_download(url: str, destination: Union[Path, str], headers: Optional[d
109109
temporary.write_bytes(b'') # clearing temporary file when total_length is inconsistent
110110

111111
with temporary.open('ab') as f:
112-
done = False
113112
downloaded = f.tell()
114113
if downloaded != 0:
115114
log.warning(f'Found a partial download {temporary}')
116115
with tqdm(initial=downloaded, total=total_length, unit='B', unit_scale=True) as pbar:
117-
while not done:
116+
while True:
118117
if downloaded != 0:
119118
log.warning(f'Download stopped abruptly, trying to resume from {downloaded} '
120119
f'to reach {total_length}')
121120
headers['Range'] = f'bytes={downloaded}-'
122121
r = requests.get(url, headers=headers, stream=True)
123122
if 'content-length' not in r.headers or \
124123
total_length - downloaded != int(r.headers['content-length']):
125-
raise RuntimeError(f'It looks like the server does not support resuming '
126-
f'downloads.')
127-
for chunk in r.iter_content(chunk_size=chunk_size):
128-
if chunk: # filter out keep-alive new chunks
129-
downloaded += len(chunk)
130-
pbar.update(len(chunk))
131-
f.write(chunk)
124+
raise RuntimeError('It looks like the server does not support resuming downloads.')
125+
126+
try:
127+
for chunk in r.iter_content(chunk_size=chunk_size):
128+
if chunk: # filter out keep-alive new chunks
129+
downloaded += len(chunk)
130+
pbar.update(len(chunk))
131+
f.write(chunk)
132+
except requests.exceptions.ChunkedEncodingError:
133+
if downloaded == 0:
134+
r = requests.get(url, stream=True, headers=headers)
135+
132136
if downloaded >= total_length:
133137
# Note that total_length is 0 if the server didn't return the content length,
134138
# in this case we perform just one iteration and assume that we are done.
135-
done = True
139+
break
136140

137141
temporary.rename(destination)
138142

0 commit comments

Comments
 (0)