diff --git a/reflex/app.py b/reflex/app.py index 584b8a321..abf0b5d41 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -64,7 +64,7 @@ from reflex.components.core.client_side_routing import ( ) from reflex.components.core.upload import Upload, get_upload_dir from reflex.components.radix import themes -from reflex.config import get_config +from reflex.config import environment, get_config from reflex.event import Event, EventHandler, EventSpec, window_alert from reflex.model import Model, get_db_status from reflex.page import ( @@ -957,15 +957,16 @@ class App(MiddlewareMixin, LifespanMixin, Base): executor = None if ( platform.system() in ("Linux", "Darwin") - and os.environ.get("REFLEX_COMPILE_PROCESSES") is not None + and (number_of_processes := environment.REFLEX_COMPILE_PROCESSES) + is not None ): executor = concurrent.futures.ProcessPoolExecutor( - max_workers=int(os.environ.get("REFLEX_COMPILE_PROCESSES", 0)) or None, + max_workers=number_of_processes, mp_context=multiprocessing.get_context("fork"), ) else: executor = concurrent.futures.ThreadPoolExecutor( - max_workers=int(os.environ.get("REFLEX_COMPILE_THREADS", 0)) or None, + max_workers=environment.REFLEX_COMPILE_THREADS ) with executor: diff --git a/reflex/config.py b/reflex/config.py index 0845528cf..f4b4e3245 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -180,6 +180,24 @@ def interpret_boolean_env(value: str) -> bool: raise ValueError(f"Invalid boolean value: {value}") +def interpret_int_env(value: str) -> int: + """Interpret an integer environment variable value. + + Args: + value: The environment variable value. + + Returns: + The interpreted value. + + Raises: + ValueError: If the value is invalid. + """ + try: + return int(value) + except ValueError as ve: + raise ValueError(f"Invalid integer value: {value}") from ve + + def interpret_path_env(value: str) -> Path: """Interpret a path environment variable value. @@ -209,10 +227,10 @@ def interpret_env_var_value(value: str, field: dataclasses.Field) -> Any: if field_type is bool: return interpret_boolean_env(value) - elif field_type is str: return value - + elif field_type is int: + return interpret_int_env(value) elif field_type is Path: return interpret_path_env(value) @@ -259,6 +277,12 @@ class EnvironmentVariables: # The directory to store uploaded files. REFLEX_UPLOADED_FILES_DIR: Path = Path(constants.Dirs.UPLOADED_FILES) + # Whether to use seperate processes to compile the frontend and how many. If not set, defaults to thread executor. + REFLEX_COMPILE_PROCESSES: Optional[int] = None + + # Whether to use seperate threads to compile the frontend and how many. Defaults to `min(32, os.cpu_count() + 4)`. + REFLEX_COMPILE_THREADS: Optional[int] = None + def __init__(self): """Initialize the environment variables.""" type_hints = get_type_hints(type(self))