Skip to content

Commit

Permalink
Merge pull request #2291 from obfusk/patch-9
Browse files Browse the repository at this point in the history
download_file: show error + exponential sleep
  • Loading branch information
AndreMiras authored Aug 11, 2020
2 parents f2946aa + 0039087 commit 6235e93
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
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

0 comments on commit 6235e93

Please sign in to comment.