python语法糖之装饰器try-catch**
#!/usr/bin/env python
# encoding: utf-8
import functools
import sys
from loguru import logger
from utils.alarm_tool import Alarm
def exception_warpper(func):
@functools.wraps(func)
def inner(*args, **kwargs):
exp_msg = ''
ret = None
try:
try:
func_name = func.__name__
file_name = func.__code__.co_filename
line_num = func.__code__.co_firstlineno
exp_msg = '{}:{},f:{}'.format(
file_name, line_num, func_name
)
except:
pass
ret = func(*args, **kwargs)
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
logger.exception(e)
Alarm.msg('exception, {}, e:{}'.format(exp_msg, exc_value))
return ret
return inner
alarm报警模块
class Alarm(object):
@classmethod
def msg(cls, txt):
ret = False
try:
url = "xxx"
payload = {
"tousers": ["lijinze1"],
"msgtype": "text",
"text": {
"content": txt
}
}
requests.request("POST", url, json=payload, timeout=10)
ret = True
except Exception as e:
logger.exception(e)
return ret
@classmethod
def call(cls, txt):
ret = False
try:
url = "xxx"
payload = {
"tousers": ["lijinze1"],
"msgtype": "voice",
"voice": {
"message": txt
}
}
requests.request("POST", url, json=payload, timeout=10)
ret = True
except Exception as e:
logger.exception(e)
return ret
当python多线程遇到异常处理
def _crawl(self):
crawler_dir = os.path.join(spider_dir, 'crawler', 'crawler_*.py')
with ThreadPoolExecutor(max_workers=self.max_worker) as pool:
futures = set()
for cls_path in glob.glob(crawler_dir):
imp_cls = self._load_class(
cls_path,
'spider.crawler',
'CrawlerImpl'
)
# 加载方法对象结束
if imp_cls is not None:
cls_obj = imp_cls()
job = pool.submit(exception_warpper(cls_obj.crawl))
futures.add(job)
else:
logger.error('spider:{} load fail!', cls_path)
for job in as_completed(futures):
result = job.result()
if result:
self.crawler_result_list.append(result)
logger.info('crawl:{}, {}', result.name, result.url)
else:
logger.warning('crawl_result is null')