thread_pool.py 824 B

1234567891011121314151617181920212223
  1. from concurrent.futures import ThreadPoolExecutor
  2. # 创建公用线程池
  3. class ThreadPoolSingleton:
  4. _instance = None
  5. _executor = None
  6. # 获取公用线程池类方法
  7. # max_workers 默认的工作线程数为2
  8. # thread_name_prefix 默认的工作线程名称前缀为 major-thread-pool-
  9. # queue_size 默认的等待队列大小为 不限制
  10. # reject_handler 默认的拒绝策略为空
  11. @classmethod
  12. def get_executor(cls, max_workers=2, thread_name_prefix='major-thread-pool-'):
  13. if cls._executor is None:
  14. cls._executor = ThreadPoolExecutor(max_workers=max_workers, thread_name_prefix=thread_name_prefix)
  15. return cls._executor
  16. def __new__(cls):
  17. if not cls._instance:
  18. cls._instance = super().__new__(cls)
  19. return cls._instance