1. 程式人生 > >一個有界任務隊列的thradpoolexcutor, 直接捕獲錯誤日誌

一個有界任務隊列的thradpoolexcutor, 直接捕獲錯誤日誌

分布 ember print res erro nbsp sch count() queue

基於官方的需要改版

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)

一個有界任務隊列的thradpoolexcutor, 直接捕獲錯誤日誌