一个有界任务队列的python thradpoolexcutor, 直接捕获错误日志
基于官方的需要改版
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)
反对极端面向过程编程思维方式,喜欢面向对象和设计模式的解读,喜欢对比极端面向过程编程和oop编程消耗代码代码行数的区别和原因。致力于使用oop和36种设计模式写出最高可复用的框架级代码和使用最少的代码行数完成任务,致力于使用oop和设计模式来使部分代码减少90%行,使绝大部分py文件最低减少50%-80%行的写法。