|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from concurrent.futures import ThreadPoolExecutor |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +from .app_setting_manager import get_app_setting |
| 7 | +from .constants import ( |
| 8 | + PYTHON_THREADPOOL_THREAD_COUNT, |
| 9 | + PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT, |
| 10 | + PYTHON_THREADPOOL_THREAD_COUNT_MIN, |
| 11 | + PYTHON_THREADPOOL_THREAD_COUNT_MAX, |
| 12 | +) |
| 13 | +from ..logging import logger |
| 14 | + |
| 15 | +_threadpool_executor: Optional[ThreadPoolExecutor] = None |
| 16 | + |
| 17 | + |
| 18 | +def _validate_thread_count(value: str) -> bool: |
| 19 | + try: |
| 20 | + int_value = int(value) |
| 21 | + except ValueError: |
| 22 | + logger.warning('%s must be an integer', PYTHON_THREADPOOL_THREAD_COUNT) |
| 23 | + return False |
| 24 | + |
| 25 | + if (int_value < PYTHON_THREADPOOL_THREAD_COUNT_MIN |
| 26 | + or int_value > PYTHON_THREADPOOL_THREAD_COUNT_MAX): |
| 27 | + logger.warning( |
| 28 | + '%s must be set to a value between %s and %s. Reverting to ' |
| 29 | + 'default value (%s).', |
| 30 | + PYTHON_THREADPOOL_THREAD_COUNT, |
| 31 | + PYTHON_THREADPOOL_THREAD_COUNT_MIN, |
| 32 | + PYTHON_THREADPOOL_THREAD_COUNT_MAX, |
| 33 | + PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT, |
| 34 | + ) |
| 35 | + return False |
| 36 | + return True |
| 37 | + |
| 38 | + |
| 39 | +def _get_max_workers() -> Optional[int]: |
| 40 | + threadpool_count = get_app_setting( |
| 41 | + setting=PYTHON_THREADPOOL_THREAD_COUNT, |
| 42 | + validator=_validate_thread_count, |
| 43 | + ) |
| 44 | + if threadpool_count is None: |
| 45 | + return None |
| 46 | + try: |
| 47 | + return int(threadpool_count) |
| 48 | + except (TypeError, ValueError) as e: |
| 49 | + logger.warning( |
| 50 | + 'Failed to convert %s value "%s" to integer: %s', |
| 51 | + PYTHON_THREADPOOL_THREAD_COUNT, threadpool_count, e |
| 52 | + ) |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def start_threadpool_executor() -> None: |
| 57 | + global _threadpool_executor |
| 58 | + max_workers = _get_max_workers() |
| 59 | + |
| 60 | + if _threadpool_executor is not None: |
| 61 | + try: |
| 62 | + _threadpool_executor.shutdown(wait=False) |
| 63 | + except Exception: |
| 64 | + pass |
| 65 | + |
| 66 | + _threadpool_executor = ThreadPoolExecutor(max_workers=max_workers) |
| 67 | + logger.debug( |
| 68 | + 'Started threadpool executor (id=%s) with max_workers=%s', |
| 69 | + id(_threadpool_executor), |
| 70 | + max_workers, |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def stop_threadpool_executor() -> None: |
| 75 | + global _threadpool_executor |
| 76 | + if _threadpool_executor is not None: |
| 77 | + try: |
| 78 | + _threadpool_executor.shutdown(wait=True) |
| 79 | + logger.debug('Stopped threadpool executor (id=%s)', |
| 80 | + id(_threadpool_executor)) |
| 81 | + finally: |
| 82 | + _threadpool_executor = None |
| 83 | + |
| 84 | + |
| 85 | +def get_threadpool_executor() -> Optional[ThreadPoolExecutor]: |
| 86 | + return _threadpool_executor |
0 commit comments