一个有界任务队列的thradpoolexcutor, 直接捕获错误日志
2024-10-11 07:21:07
基于官方的需要改版
1、改为有界,官方是吧所有任务添加到线程池的queue队列中,这样内存会变大,也不符合分布式的逻辑(会把中间件的所有任务一次性取完,放到本地的queue队列中,导致分布式变差)
2、直接打印错误。官方的threadpolexcutor执行的函数,如果不设置回调,即使函数中出错了,自己都不会知道。
# coding=utf-8
"""
一个有界任务队列的thradpoolexcutor
直接捕获错误日志
"""
from functools import wraps
import queue
from concurrent.futures import ThreadPoolExecutor, Future
# noinspection PyProtectedMember
from concurrent.futures.thread import _WorkItem
from app.utils_ydf import LoggerMixin, LogManager logger = LogManager('BoundedThreadPoolExecutor').get_logger_and_add_handlers() def _deco(f):
@wraps(f)
def __deco(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
logger.exception(e) return __deco class BoundedThreadPoolExecutor(ThreadPoolExecutor, ):
def __init__(self, max_workers=None, thread_name_prefix=''):
ThreadPoolExecutor.__init__(self, max_workers, thread_name_prefix)
self._work_queue = queue.Queue(max_workers * 2) def submit(self, fn, *args, **kwargs):
with self._shutdown_lock:
if self._shutdown:
raise RuntimeError('cannot schedule new futures after shutdown')
f = Future()
fn_deco = _deco(fn)
w = _WorkItem(f, fn_deco, args, kwargs)
self._work_queue.put(w)
self._adjust_thread_count()
return f if __name__ == '__main__':
def fun():
print(1 / 0) pool = BoundedThreadPoolExecutor(10)
pool.submit(fun)
最新文章
- 映射一对多双向关联关系 cascade、inverse、属性
- 友好解决POI导入Excel文件行是不是为空
- dom4j读写XML文件
- php number_format()保留小数点后几位
- PHP学习笔记(五)
- 【CF】283D Tennis Game
- WCF - 学习总目录
- Window.onload事件
- phpcms添加视频
- HTML5_input_file_打开很慢的问题
- 新浪新闻页面抓取(JAVA-Jsoup)
- Jenkins 的安装部署
- 网站内容js设置 禁止复制,禁止选择
- Python-递归复习-斐波那契-阶乘-52
- Ubuntu16.04下修改MySQL数据的默认存储位置
- Maven整理
- cxgrid合并值相同的某列
- mysql中的 随机字符串的生成
- Vue项目中引用vue-resource步骤
- iOS开发中的地图开发
热门文章
- Centos 6 安装 Mysql 5.6
- java第七周动手动脑
- MySQL数据库引擎MyISAM和InnoDB的区别介绍
- 数据可视化Echarts-实例
- [CentOS7]redis设置开机启动,设置密码
- Linux下chkconfig命令
- redis 简单使用总结
- cd4与cd8比值的意义
- exception:Failed to execute 'toDataURL' on 'HTMLCanvasElement' 解决方案
- Xcode9.2打包图片显示异常解决方案