基于官方的需要改版

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)

最新文章

  1. 映射一对多双向关联关系 cascade、inverse、属性
  2. 友好解决POI导入Excel文件行是不是为空
  3. dom4j读写XML文件
  4. php number_format()保留小数点后几位
  5. PHP学习笔记(五)
  6. 【CF】283D Tennis Game
  7. WCF - 学习总目录
  8. Window.onload事件
  9. phpcms添加视频
  10. HTML5_input_file_打开很慢的问题
  11. 新浪新闻页面抓取(JAVA-Jsoup)
  12. Jenkins 的安装部署
  13. 网站内容js设置 禁止复制,禁止选择
  14. Python-递归复习-斐波那契-阶乘-52
  15. Ubuntu16.04下修改MySQL数据的默认存储位置
  16. Maven整理
  17. cxgrid合并值相同的某列
  18. mysql中的 随机字符串的生成
  19. Vue项目中引用vue-resource步骤
  20. iOS开发中的地图开发

热门文章

  1. Centos 6 安装 Mysql 5.6
  2. java第七周动手动脑
  3. MySQL数据库引擎MyISAM和InnoDB的区别介绍
  4. 数据可视化Echarts-实例
  5. [CentOS7]redis设置开机启动,设置密码
  6. Linux下chkconfig命令
  7. redis 简单使用总结
  8. cd4与cd8比值的意义
  9. exception:Failed to execute 'toDataURL' on 'HTMLCanvasElement' 解决方案
  10. Xcode9.2打包图片显示异常解决方案