app_module_for_backend.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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.exec import is_prod_mode
  7. from reflex.utils.prerequisites import get_app
  8. if "app" != constants.CompileVars.APP:
  9. raise AssertionError("unexpected variable name for 'app'")
  10. app_module = get_app(reload=False)
  11. app = getattr(app_module, constants.CompileVars.APP)
  12. # For py3.8 and py3.9 compatibility when redis is used, we MUST add any decorator pages
  13. # before compiling the app in a thread to avoid event loop error (REF-2172).
  14. app._apply_decorated_pages()
  15. compile_future = ThreadPoolExecutor(max_workers=1).submit(app._compile)
  16. compile_future.add_done_callback(
  17. # Force background compile errors to print eagerly
  18. lambda f: f.result()
  19. )
  20. # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted.
  21. if is_prod_mode():
  22. compile_future.result()
  23. # ensure only "app" is exposed.
  24. del app_module
  25. del compile_future
  26. del get_app
  27. del is_prod_mode
  28. del constants
  29. del ThreadPoolExecutor