GlobalThreadPoolObject.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : GlobalThreadPoolObject.py
  4. @Time : 2024/8/20 19:49
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import threading
  10. from concurrent.futures import ThreadPoolExecutor
  11. from AnsjerPush.config import CONFIG_EUR, CONFIG_INFO, CONFIG_CN, CONFIG_US
  12. class GlobalThreadPool:
  13. _instance = None
  14. _lock = threading.Lock()
  15. def __new__(cls):
  16. if not cls._instance:
  17. with cls._lock:
  18. if not cls._instance:
  19. cls._instance = super(GlobalThreadPool, cls).__new__(cls)
  20. max_workers = 20
  21. if CONFIG_INFO == CONFIG_US or CONFIG_INFO == CONFIG_EUR:
  22. max_workers = 300
  23. elif CONFIG_INFO == CONFIG_CN:
  24. max_workers = 100
  25. cls._instance.executor = ThreadPoolExecutor(
  26. max_workers=max_workers,
  27. thread_name_prefix="global-thread-pool"
  28. )
  29. return cls._instance
  30. def submit(self, fn, *args, **kwargs):
  31. return self.executor.submit(fn, *args, **kwargs)
  32. def shutdown(self, wait=True):
  33. self.executor.shutdown(wait)