-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
36 lines (31 loc) · 984 Bytes
/
utils.py
File metadata and controls
36 lines (31 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import logging
import time
import traceback
from functools import wraps
logger = logging.getLogger(__name__)
def retry(retry_times=3, fixed_sleep=10, retry_on_exception=Exception):
"""
重试装饰器
:param func:
:param retry_times:
:param fixed_sleep:
:param retry_on_exception:
:return:
"""
def decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
for _ in range(retry_times):
try:
return func(*args, **kwargs)
except retry_on_exception as e:
logger.info(f'{func} retrying {retry_times} times')
if fixed_sleep > 0:
time.sleep(fixed_sleep)
try:
func(*args, **kwargs)
except retry_on_exception:
logger.error(f'{func} retry failed')
raise RuntimeError(traceback.format_exc())
return wrapped
return decorator