From a72208197589af338d9fd00e23ce35af4d109738 Mon Sep 17 00:00:00 2001 From: g0ldyy <153996346+g0ldyy@users.noreply.github.com> Date: Sun, 4 Jan 2026 23:48:33 +0100 Subject: [PATCH] refactor: centralize `ProcessPoolExecutor` worker count calculation to a shared module-level variable --- comet/core/execution.py | 10 ++++------ comet/core/logger.py | 8 ++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/comet/core/execution.py b/comet/core/execution.py index 83e8ed6..d4f77c0 100644 --- a/comet/core/execution.py +++ b/comet/core/execution.py @@ -13,6 +13,10 @@ except ValueError: _mp_context = multiprocessing.get_context("spawn") app_executor = None +max_workers = settings.EXECUTOR_MAX_WORKERS +if max_workers is None: + cpu_count = os.cpu_count() or 1 + max_workers = min(cpu_count, 4) def worker_initializer(): @@ -22,12 +26,6 @@ def worker_initializer(): def setup_executor(): global app_executor - max_workers = settings.EXECUTOR_MAX_WORKERS - - if max_workers is None: - cpu_count = os.cpu_count() or 1 - max_workers = min(cpu_count, 4) - app_executor = ProcessPoolExecutor( max_workers=max_workers, mp_context=_mp_context, initializer=worker_initializer ) diff --git a/comet/core/logger.py b/comet/core/logger.py index 955a967..0a6cbee 100644 --- a/comet/core/logger.py +++ b/comet/core/logger.py @@ -1,11 +1,11 @@ import logging -import os import re import sys import time from loguru import logger +from comet.core.execution import max_workers from comet.core.log_levels import (CUSTOM_LOG_LEVELS, STANDARD_LOG_LEVELS, get_level_info) @@ -177,13 +177,9 @@ def log_startup_info(settings): ) logger.log("COMET", f"Gunicorn Preload App: {settings.GUNICORN_PRELOAD_APP}") - executor_workers = settings.EXECUTOR_MAX_WORKERS - if executor_workers is None: - cpu_count = os.cpu_count() or 1 - executor_workers = min(cpu_count, 4) logger.log( "COMET", - f"ProcessPoolExecutor: {executor_workers} workers {'(auto)' if settings.EXECUTOR_MAX_WORKERS is None else ''}", + f"ProcessPoolExecutor: {max_workers} workers {'(auto)' if settings.EXECUTOR_MAX_WORKERS is None else ''}", ) if settings.PUBLIC_BASE_URL: