# -*- encoding: utf-8 -*- """ @File : GlobalThreadPoolObject.py @Time : 2024/8/20 19:49 @Author : stephen @Email : zhangdongming@asj6.wecom.work @Software: PyCharm """ # GlobalThreadPoolObject.py import threading from concurrent.futures import ThreadPoolExecutor from AnsjerPush.config import CONFIG_EUR, CONFIG_INFO, CONFIG_CN, CONFIG_US class GlobalThreadPool: _instance = None _lock = threading.Lock() def __new__(cls): if not cls._instance: with cls._lock: if not cls._instance: cls._instance = super(GlobalThreadPool, cls).__new__(cls) max_workers = 20 if CONFIG_INFO in (CONFIG_US, CONFIG_EUR): max_workers = 100 elif CONFIG_INFO == CONFIG_CN: max_workers = 80 cls._instance.executor = ThreadPoolExecutor( max_workers=max_workers, thread_name_prefix="global-thread-pool" ) return cls._instance def submit(self, fn, *args, **kwargs): try: return self.executor.submit(fn, *args, **kwargs) except RuntimeError as e: # 线程池满了等异常,抛给外面去兜底(回退机制) raise e def shutdown(self, wait=True): self.executor.shutdown(wait)