zoukankan      html  css  js  c++  java
  • 一个有界任务队列的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)
  • 相关阅读:
    UI是一个状态机
    WPF : Binding的3个属性: Source, RelativeSource, ElementName
    业务驱动设计
    WPF : 对Collection要注意重用子控件
    WPF : UserControl的Initialized事件不会触发
    mvc3上传图片
    MVC3.0自定义视图引擎(切换皮肤)
    ASP.NET MVC 多语言解决方案
    键盘键位表
    Silverlight之IsolatedStorageSettings对象
  • 原文地址:https://www.cnblogs.com/ydf0509/p/10744570.html
Copyright © 2011-2022 走看看