app_module_for_backend.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Shims the real reflex app module for running backend server (uvicorn or gunicorn).
  2. Only the app attribute is explicitly exposed.
  3. """
  4. from concurrent.futures import ThreadPoolExecutor
  5. from reflex import constants
  6. from reflex.utils import telemetry
  7. from reflex.utils.exec import is_prod_mode
  8. from reflex.utils.prerequisites import get_and_validate_app
  9. if constants.CompileVars.APP != "app":
  10. raise AssertionError("unexpected variable name for 'app'")
  11. telemetry.send("compile")
  12. app, app_module = get_and_validate_app(reload=False)
  13. # For py3.9 compatibility when redis is used, we MUST add any decorator pages
  14. # before compiling the app in a thread to avoid event loop error (REF-2172).
  15. app._apply_decorated_pages()
  16. compile_future = ThreadPoolExecutor(max_workers=1).submit(app._compile)
  17. compile_future.add_done_callback(
  18. # Force background compile errors to print eagerly
  19. lambda f: f.result()
  20. )
  21. # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted.
  22. if is_prod_mode():
  23. compile_future.result()
  24. # ensure only "app" is exposed.
  25. del app_module
  26. del compile_future
  27. del get_and_validate_app
  28. del is_prod_mode
  29. del telemetry
  30. del constants
  31. del ThreadPoolExecutor