From fc7c1bd2de0e9413b35bdbda9849441079b75ee8 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 22 Jun 2020 22:39:28 -0700 Subject: [PATCH] prevent loading un-necssary modules when using a handled, thanks Paolo --- gluon/main.py | 39 ++++++++++++++++++------------------ handlers/cgihandler.py | 3 +++ handlers/fcgihandler.py | 4 +++- handlers/gaehandler.py | 9 +++++---- handlers/isapiwsgihandler.py | 2 ++ handlers/modpythonhandler.py | 3 +++ handlers/scgihandler.py | 4 +++- handlers/web2py_on_gevent.py | 2 ++ handlers/wsgihandler.py | 4 +++- 9 files changed, 43 insertions(+), 27 deletions(-) diff --git a/gluon/main.py b/gluon/main.py index 8f5fac1b..ee1781ae 100644 --- a/gluon/main.py +++ b/gluon/main.py @@ -56,19 +56,18 @@ create_missing_folders() # set up logging for subsequent imports import logging.config -# This needed to prevent exception on Python 2.5: -# NameError: name 'gluon' is not defined -# See http://bugs.python.org/issue1436 - -# attention!, the import Tkinter in messageboxhandler, changes locale ... -import gluon.messageboxhandler -logging.gluon = gluon -# so we must restore it! Thanks ozancag -import locale -locale.setlocale(locale.LC_CTYPE, "C") # IMPORTANT, web2py requires locale "C" - -exists = os.path.exists -pjoin = os.path.join +# do not need fancy graphical loggers when served by handler +# (e.g. apache + mod_wsgi), speed up execution +if not global_settings.web2py_runtime_handler: + # attention!, the import Tkinter in messageboxhandler, changes locale ... + import gluon.messageboxhandler + # This needed to prevent exception on Python 2.5: + # NameError: name 'gluon' is not defined + # See http://bugs.python.org/issue1436 + logging.gluon = gluon + # so we must restore it! Thanks ozancag + import locale + locale.setlocale(locale.LC_CTYPE, "C") # IMPORTANT, web2py requires locale "C" try: logging.config.fileConfig(abspath("logging.conf")) @@ -76,6 +75,9 @@ except: # fails on GAE or when logfile is missing logging.basicConfig() logger = logging.getLogger("web2py") +exists = os.path.exists +pjoin = os.path.join + from gluon.restricted import RestrictedError from gluon.http import HTTP, redirect from gluon.globals import Request, Response, Session @@ -99,17 +101,14 @@ requests = 0 # gc timer try: version_info = read_file(pjoin(global_settings.gluon_parent, 'VERSION')) - raw_version_string = version_info.split()[-1].strip() - global_settings.web2py_version = raw_version_string - web2py_version = global_settings.web2py_version + web2py_version = global_settings.web2py_version = version_info.split()[-1].strip() except: raise RuntimeError("Cannot determine web2py version") -try: +# do not need rocket nor HttpServer when served by handler +# (e.g. apache + mod_wsgi), speed up execution and save memory +if not global_settings.web2py_runtime_handler: from gluon import rocket -except: - if not global_settings.web2py_runtime_gae: - logger.warn('unable to import Rocket') load_routes() diff --git a/handlers/cgihandler.py b/handlers/cgihandler.py index bd5bdf92..860056ea 100755 --- a/handlers/cgihandler.py +++ b/handlers/cgihandler.py @@ -62,6 +62,9 @@ if not os.path.isdir('applications'): sys.path = [path] + [p for p in sys.path if not p == path] +from gluon.settings import global_settings +global_settings.web2py_runtime_handler = True + import gluon.main wsgiref.handlers.CGIHandler().run(gluon.main.wsgibase) diff --git a/handlers/fcgihandler.py b/handlers/fcgihandler.py index a9b047e2..b38c703f 100755 --- a/handlers/fcgihandler.py +++ b/handlers/fcgihandler.py @@ -40,6 +40,9 @@ if not os.path.isdir('applications'): sys.path = [path] + [p for p in sys.path if not p == path] +from gluon.settings import global_settings +global_settings.web2py_runtime_handler = True + import gluon.main import gluon.contrib.gateways.fcgi as fcgi @@ -51,7 +54,6 @@ else: application = gluon.main.wsgibase if SOFTCRON: - from gluon.settings import global_settings global_settings.web2py_crontype = 'soft' fcgi.WSGIServer(application, bindAddress='/tmp/fcgi.sock').run() diff --git a/handlers/gaehandler.py b/handlers/gaehandler.py index 08aa4e22..1704d2ae 100755 --- a/handlers/gaehandler.py +++ b/handlers/gaehandler.py @@ -49,14 +49,15 @@ from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app +global_settings.web2py_runtime_handler = True global_settings.web2py_runtime_gae = True global_settings.db_sessions = True if os.environ.get('SERVER_SOFTWARE', '').startswith('Devel'): - (global_settings.web2py_runtime, DEBUG) = \ - ('gae:development', True) + global_settings.web2py_runtime = 'gae:development' + DEBUG = True else: - (global_settings.web2py_runtime, DEBUG) = \ - ('gae:production', False) + global_settings.web2py_runtime = 'gae:production' + DEBUG = False import gluon.main diff --git a/handlers/isapiwsgihandler.py b/handlers/isapiwsgihandler.py index 02755150..025eb266 100644 --- a/handlers/isapiwsgihandler.py +++ b/handlers/isapiwsgihandler.py @@ -13,6 +13,8 @@ def __ExtensionFactory__(): if not os.path.isdir('applications'): raise RuntimeError('Running from the wrong folder') sys.path = [path] + [p for p in sys.path if not p == path] + from gluon.settings import global_settings + global_settings.web2py_runtime_handler = True import gluon.main import isapi_wsgi application = gluon.main.wsgibase diff --git a/handlers/modpythonhandler.py b/handlers/modpythonhandler.py index 388f37eb..0903e2ef 100755 --- a/handlers/modpythonhandler.py +++ b/handlers/modpythonhandler.py @@ -40,6 +40,9 @@ if not os.path.isdir('applications'): sys.path = [path] + [p for p in sys.path if not p == path] +from gluon.settings import global_settings +global_settings.web2py_runtime_handler = True + import gluon.main diff --git a/handlers/scgihandler.py b/handlers/scgihandler.py index 90b81fcd..9f18607a 100755 --- a/handlers/scgihandler.py +++ b/handlers/scgihandler.py @@ -53,6 +53,9 @@ if not os.path.isdir('applications'): sys.path = [path] + [p for p in sys.path if not p == path] +from gluon.settings import global_settings +global_settings.web2py_runtime_handler = True + import gluon.main # uncomment one of the two imports below depending on the SCGIWSGI server installed @@ -70,7 +73,6 @@ else: application = wsgiapp if SOFTCRON: - from gluon.settings import global_settings global_settings.web2py_crontype = 'soft' # uncomment one of the two rows below depending on the SCGIWSGI server installed diff --git a/handlers/web2py_on_gevent.py b/handlers/web2py_on_gevent.py index 40a0ff6a..971beeb7 100644 --- a/handlers/web2py_on_gevent.py +++ b/handlers/web2py_on_gevent.py @@ -21,6 +21,8 @@ from gevent import monkey monkey.patch_all() def run(options): + from gluon.settings import global_settings + global_settings.web2py_runtime_handler = True import gluon.main if options.password != '': gluon.main.save_password(options.password, int(options.port)) diff --git a/handlers/wsgihandler.py b/handlers/wsgihandler.py index e4f19c9c..9c98619f 100644 --- a/handlers/wsgihandler.py +++ b/handlers/wsgihandler.py @@ -26,6 +26,9 @@ if not os.path.isdir('applications'): sys.path = [path] + [p for p in sys.path if not p == path] +from gluon.settings import global_settings +global_settings.web2py_runtime_handler = True + import gluon.main if LOGGING: @@ -36,5 +39,4 @@ else: application = gluon.main.wsgibase if SOFTCRON: - from gluon.settings import global_settings global_settings.web2py_crontype = 'soft'