Commit eaa2650e authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis Committed by Christos Stavrakakis
Browse files

Implement a simple `retry' decorator

parent 03fbf84f
......@@ -111,3 +111,27 @@ def _callAstakos(token, url, headers={}, body=None,
if status < 200 or status >= 300:
raise Exception(status, data)
return simplejson.loads(unicode(data))
# ----------------------------
# A simple retry decorator
def retry(howmany):
def decorator(func):
def f(*args, **kwargs):
attemps = 0
while True:
try:
return func(*args, **kwargs)
except Exception as e:
is_last_attempt = attemps == howmany - 1
if is_last_attempt:
raise e
if e.args:
status = e[0]
# In case of Unauthorized response
# or Not Found return immediately
if status == 401 or status == 404:
raise e
attemps += 1
return f
return decorator
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment