app: recognize REFLEX_COMPILE_PROCESSES and REFLEX_COMPILE_THREADS

Control whether multiprocessing is used and the number of processes or threads
that should be used.

This will allow users to opt-in to the new, potentially hazardous,
multiprocessing mode, which results in much faster compiles, but has already
been reverted 4 times. Lets leave the code in this time, but use the thread
pool executor by default.

Limiting the number of threads or processes to 1 can also aid in debugging
issues that arise during compile time.
This commit is contained in:
Masen Furer 2024-03-11 16:19:46 -07:00
parent 38eb84498b
commit 5600bc5a30
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -860,12 +860,17 @@ class App(Base):
# Use a forking process pool, if possible. Much faster, especially for large sites.
# Fallback to ThreadPoolExecutor as something that will always work.
executor = None
if platform.system() in ("Linux", "Darwin"):
if platform.system() in ("Linux", "Darwin") and os.environ.get(
"REFLEX_COMPILE_PROCESSES"
):
executor = concurrent.futures.ProcessPoolExecutor(
mp_context=multiprocessing.get_context("fork")
max_workers=int(os.environ.get("REFLEX_COMPILE_PROCESSES", 0)) or None,
mp_context=multiprocessing.get_context("fork"),
)
else:
executor = concurrent.futures.ThreadPoolExecutor()
executor = concurrent.futures.ThreadPoolExecutor(
max_workers=int(os.environ.get("REFLEX_COMPILE_THREADS", 0)) or None,
)
with executor:
result_futures = []