Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download_file: show error + exponential sleep #2291

Merged
merged 2 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pythonforandroid/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,17 @@ def report_hook(index, blksize, size):

# Download item with multiple attempts (for bad connections):
attempts = 0
seconds = 1
while True:
try:
urlretrieve(url, target, report_hook)
except OSError:
except OSError as e:
attempts += 1
if attempts >= 5:
raise
stdout.write('Download failed retrying in a second...')
time.sleep(1)
stdout.write('Download failed: {}; retrying in {} second(s)...'.format(e, seconds))
time.sleep(seconds)
seconds *= 2
continue
break
return target
Expand Down
2 changes: 1 addition & 1 deletion tests/test_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def test_download_file_scheme_https_oserror(self):
retry = 5
expected_call_args_list = [mock.call(url, filename, mock.ANY)] * retry
assert m_urlretrieve.call_args_list == expected_call_args_list
expected_call_args_list = [mock.call(1)] * (retry - 1)
expected_call_args_list = [mock.call(2**i) for i in range(retry - 1)]
assert m_sleep.call_args_list == expected_call_args_list


Expand Down