Skip to content

Instantly share code, notes, and snippets.

Embed
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):
try:
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 for..in
except requests.exceptions.ReadTimeout as e: # FIXME: TEST
logging.warning('READ TIMEOUT %s', e.args) # args -> urllib3.exceptions.ReadTimeoutError
logging.warning(
"#Request (%s) failed (timeout %ds, code %d), retry",
params, tm, getattr(r, 'status_code', 0)
)
except requests.exceptions.ConnectionError as e:
# https://github.com/requests/requests/issues/2392
# ProtocolError=Удаленный хост принудительно разорвал существующее подключение
if not isinstance(e.args[0], (#urllib3.exceptions.ReadTimeoutError, #<-TimeoutError
urllib3.exceptions.ProtocolError,
urllib3.exceptions.TimeoutError,
urllib3.exceptions.MaxRetryError,
)):
logging.warning("DEBUG: %s %s", type(e.args[0]), e.args[0])
raise # it's not a timeout error, re-raise
logging.warning(
"#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