zhaoshengzhi
2018-05-04 16:26:19 +08:00
贴一串以前的,用 auto_retry 自动重试,用 time_limit 设置时间限制,也可以组合同时使用
import traceback
import json
import subprocess
import eventlet
eventlet.monkey_patch()
RETRY_COUNT = 10
TIMEOUT = 10
def time_limit(func, *args, **kwargs):
trace = None
for _ in range(RETRY_COUNT):
try:
with eventlet.Timeout(TIMEOUT):
return func(*args, **kwargs)
except eventlet.timeout.Timeout, e:
trace = format_traceback()
except:
trace = format_traceback()
ignore_exception = kwargs.get('ignore_exception')
if not ignore_exception:
raise Exception(
"max retry count=%d, func=%s, argv=%s, trace=%s" % (RETRY_COUNT, func.__name__, (args, kwargs), trace))
def auto_retry(func, *args, **kwargs):
trace = None
for _ in range(RETRY_COUNT):
try:
return func(*args, **kwargs)
except:
trace = format_traceback()
ignore_exception = kwargs.get('ignore_exception')
if not ignore_exception:
raise Exception(
"max retry count=%d, func=%s, argv=%s, trace=%s" % (RETRY_COUNT, func.__name__, (args, kwargs), trace))
def format_traceback():
trace = traceback.format_exc()
return json.dumps(trace, ensure_ascii=False)