app_module_for_backend.py 997 B

12345678910111213141516171819202122232425262728293031
  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, get_compiled_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. compile_future = ThreadPoolExecutor(max_workers=1).submit(app.compile_)
  13. compile_future.add_done_callback(
  14. # Force background compile errors to print eagerly
  15. lambda f: f.result()
  16. )
  17. # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted.
  18. if is_prod_mode():
  19. compile_future.result()
  20. # ensure only "app" is exposed.
  21. del app_module
  22. del compile_future
  23. del get_app
  24. del get_compiled_app
  25. del is_prod_mode
  26. del constants
  27. del ThreadPoolExecutor