Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# r = read_timeout_handler(self.session.get, self.url_card, params=params, timeout=[10, 20, 30])
def read_timeout_handler(func, url, timeout, retries=None, params={}, **kw):
"Retries on read timeout"
if not hasattr(timeout, '__iter__'):
timeout = [timeout] * (retries or 1)
elif retries is not None:
raise ValueError("retries specified along with list of timeouts")
for retry, tm in enumerate(timeout):
r = None # init
r = func(url, params=params, timeout=tm, **kw)
if retry > 0:
logging.warning("#Success (%s), %d retry(s)", params, retry)
break # exit
except requests.exceptions.ReadTimeout as e: # FIXME: TEST
logging.warning('READ TIMEOUT %s', e.args) # args -> urllib3.exceptions.ReadTimeoutError
"#Request (%s) failed (timeout %ds, code %d), retry",
params, tm, getattr(r, 'status_code', 0)
except requests.exceptions.ConnectionError as e:
# ProtocolError=Удаленный хост принудительно разорвал существующее подключение
if not isinstance(e.args[0], (#urllib3.exceptions.ReadTimeoutError, #<-TimeoutError
logging.warning("DEBUG: %s %s", type(e.args[0]), e.args[0])
raise # it's not a timeout error, re-raise
"#Request (%s) failed (timeout %ds, code %d), retry",
params, tm, getattr(r, 'status_code', 0)
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment