TypingKit
← Code

Python: Retry with backoff

Written for TypingKit · ~2 min

0 wpm 100% accuracy 100% real accuracy
import random↵
import time↵
↵
↵
def retry(attempts=5, base=0.5):↵
def wrap(fn):↵
def run(*args, **kwargs):↵
for i in range(attempts):↵
try:↵
return fn(*args, **kwargs)↵
except ConnectionError:↵
if i == attempts - 1:↵
raise↵
time.sleep(base * 2 ** i + random.random() / 10)↵
return run↵
return wrap