From 15665b9922784f40397fc7b716b8806be8badbb7 Mon Sep 17 00:00:00 2001 From: Andrew Gorcester Date: Thu, 12 Feb 2015 13:42:18 -0800 Subject: [PATCH] add middleware to monkey-patch os.env to be thread local for GAE compatibility --- multicore_runtime/wsgi.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/multicore_runtime/wsgi.py b/multicore_runtime/wsgi.py index e7146cd4..1b4de7e3 100644 --- a/multicore_runtime/wsgi.py +++ b/multicore_runtime/wsgi.py @@ -27,10 +27,10 @@ from wsgi_utils import get_module_config_filename from wsgi_utils import load_user_scripts_into_handlers +from google.appengine.ext.vmruntime import middlewares from google.appengine.ext.vmruntime import vmconfig from google.appengine.ext.vmruntime import vmstub - logging.basicConfig(level=logging.INFO) appinfo_external = get_module_config(get_module_config_filename()) @@ -49,3 +49,19 @@ # Create a "meta app" that dispatches requests based on handlers. meta_app = dispatcher(preloaded_handlers) + +# Wrap meta_app in middleware. The first statement in this section is the +# innermost layer of the middleware, and the last statement is the outermost +# layer (the middleware code that will process a request first). Inside the +# innermost layer is the actual dispatcher, above. + +# Adjust SERVER_NAME and SERVER_PORT env vars to hide the service bridge. +meta_app = middlewares.FixServerEnvVarsMiddleware(meta_app) + +# Patch os.environ to be thread local, and stamp it with default values based +# on the app configuration. This is for backwards compatibility with GAE's use +# of environment variables to store request data. +# Note: gunicorn "gevent" or "eventlet" workers, if selected, will +# automatically monkey-patch the threading module to make this compatible with +# green threads. +meta_app = middlewares.OsEnvSetupMiddleware(meta_app, appengine_config)