GlobalThreadPoolObject.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. thread_name_prefix="global-thread-pool"
  27. )
  28. return cls._instance
  29. def submit(self, fn, *args, **kwargs):
  30. return self.executor.submit(fn, *args, **kwargs)
  31. def shutdown(self, wait=True):
  32. self.executor.shutdown(wait)