Skip to content
104 changes: 1 addition & 103 deletions packages/reflex-core/src/reflex_core/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

from __future__ import annotations

import concurrent.futures
import dataclasses
import enum
import importlib
import multiprocessing
import os
import platform
from collections.abc import Callable, Sequence
from collections.abc import Sequence
from functools import lru_cache
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -529,97 +526,6 @@ class PerformanceMode(enum.Enum):
OFF = "off"


class ExecutorType(enum.Enum):
"""Executor for compiling the frontend."""

THREAD = "thread"
PROCESS = "process"
MAIN_THREAD = "main_thread"

@classmethod
def get_executor_from_environment(cls):
"""Get the executor based on the environment variables.

Returns:
The executor.
"""
from reflex_core.utils import console

executor_type = environment.REFLEX_COMPILE_EXECUTOR.get()

reflex_compile_processes = environment.REFLEX_COMPILE_PROCESSES.get()
reflex_compile_threads = environment.REFLEX_COMPILE_THREADS.get()
# By default, use the main thread. Unless the user has specified a different executor.
# Using a process pool is much faster, but not supported on all platforms. It's gated behind a flag.
if executor_type is None:
if (
platform.system() not in ("Linux", "Darwin")
and reflex_compile_processes is not None
):
console.warn("Multiprocessing is only supported on Linux and MacOS.")

if (
platform.system() in ("Linux", "Darwin")
and reflex_compile_processes is not None
):
if reflex_compile_processes == 0:
console.warn(
"Number of processes must be greater than 0. If you want to use the default number of processes, set REFLEX_COMPILE_EXECUTOR to 'process'. Defaulting to None."
)
reflex_compile_processes = None
elif reflex_compile_processes < 0:
console.warn(
"Number of processes must be greater than 0. Defaulting to None."
)
reflex_compile_processes = None
executor_type = ExecutorType.PROCESS
elif reflex_compile_threads is not None:
if reflex_compile_threads == 0:
console.warn(
"Number of threads must be greater than 0. If you want to use the default number of threads, set REFLEX_COMPILE_EXECUTOR to 'thread'. Defaulting to None."
)
reflex_compile_threads = None
elif reflex_compile_threads < 0:
console.warn(
"Number of threads must be greater than 0. Defaulting to None."
)
reflex_compile_threads = None
executor_type = ExecutorType.THREAD
else:
executor_type = ExecutorType.MAIN_THREAD

match executor_type:
case ExecutorType.PROCESS:
executor = concurrent.futures.ProcessPoolExecutor(
max_workers=reflex_compile_processes,
mp_context=multiprocessing.get_context("fork"),
)
case ExecutorType.THREAD:
executor = concurrent.futures.ThreadPoolExecutor(
max_workers=reflex_compile_threads
)
case ExecutorType.MAIN_THREAD:
FUTURE_RESULT_TYPE = TypeVar("FUTURE_RESULT_TYPE")

class MainThreadExecutor:
def __enter__(self):
return self

def __exit__(self, *args):
pass

def submit(
self, fn: Callable[..., FUTURE_RESULT_TYPE], *args, **kwargs
) -> concurrent.futures.Future[FUTURE_RESULT_TYPE]:
future_job = concurrent.futures.Future()
future_job.set_result(fn(*args, **kwargs))
return future_job

executor = MainThreadExecutor()

return executor


class EnvironmentVariables:
"""Environment variables class to instantiate environment variables."""

Expand Down Expand Up @@ -660,14 +566,6 @@ class EnvironmentVariables:
Path(constants.Dirs.UPLOADED_FILES)
)

REFLEX_COMPILE_EXECUTOR: EnvVar[ExecutorType | None] = env_var(None)

# Whether to use separate processes to compile the frontend and how many. If not set, defaults to thread executor.
REFLEX_COMPILE_PROCESSES: EnvVar[int | None] = env_var(None)

# Whether to use separate threads to compile the frontend and how many. Defaults to `min(32, os.cpu_count() + 4)`.
REFLEX_COMPILE_THREADS: EnvVar[int | None] = env_var(None)

# The directory to store reflex dependencies.
REFLEX_DIR: EnvVar[Path] = env_var(constants.Reflex.DIR)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ asyncio_mode = "auto"

[tool.codespell]
skip = "docs/*,*.html,examples/*, *.pyi, poetry.lock, uv.lock"
ignore-words-list = "te, TreeE, selectin"
ignore-words-list = "te, TreeE, selectin, asend"


[tool.coverage.run]
Expand Down
Loading
Loading