From f117db24a9e851ab270d517a6ad252f9340ff798 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Thu, 9 May 2024 16:39:36 -0700 Subject: [PATCH] Parse flags in wsgi_app --- mesop/server/wsgi_app.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mesop/server/wsgi_app.py b/mesop/server/wsgi_app.py index 1024802a9..78db61354 100644 --- a/mesop/server/wsgi_app.py +++ b/mesop/server/wsgi_app.py @@ -1,5 +1,7 @@ +import sys from typing import Any, Callable +from absl import flags from flask import Flask from mesop.runtime import enable_debug_mode @@ -43,6 +45,19 @@ def create_app( return App(flask_app=flask_app) +# Note: we are lazily instantiating this because this module may be +# imported in contexts which will create the flask app elsewhere. +_app = None + + def wsgi_app(environ: dict[Any, Any], start_response: Callable[..., Any]): - app = create_app(prod_mode=True) - return app._flask_app.wsgi_app(environ, start_response) # type: ignore + # Lazily create and reuse a flask app singleton to avoid + # the overhead for each WSGI request. + global _app + if not _app: + # Parse the flags before creating the app otherwise you will + # get UnparsedFlagAccessError. + flags.FLAGS(sys.argv) + _app = create_app(prod_mode=True) + + return _app._flask_app.wsgi_app(environ, start_response) # type: ignore