prevent loading un-necssary modules when using a handled, thanks Paolo

This commit is contained in:
mdipierro
2020-06-22 22:39:28 -07:00
parent cf76978a39
commit fc7c1bd2de
9 changed files with 43 additions and 27 deletions
+19 -20
View File
@@ -56,19 +56,18 @@ create_missing_folders()
# set up logging for subsequent imports # set up logging for subsequent imports
import logging.config import logging.config
# This needed to prevent exception on Python 2.5: # do not need fancy graphical loggers when served by handler
# NameError: name 'gluon' is not defined # (e.g. apache + mod_wsgi), speed up execution
# See http://bugs.python.org/issue1436 if not global_settings.web2py_runtime_handler:
# attention!, the import Tkinter in messageboxhandler, changes locale ...
# attention!, the import Tkinter in messageboxhandler, changes locale ... import gluon.messageboxhandler
import gluon.messageboxhandler # This needed to prevent exception on Python 2.5:
logging.gluon = gluon # NameError: name 'gluon' is not defined
# so we must restore it! Thanks ozancag # See http://bugs.python.org/issue1436
import locale logging.gluon = gluon
locale.setlocale(locale.LC_CTYPE, "C") # IMPORTANT, web2py requires locale "C" # so we must restore it! Thanks ozancag
import locale
exists = os.path.exists locale.setlocale(locale.LC_CTYPE, "C") # IMPORTANT, web2py requires locale "C"
pjoin = os.path.join
try: try:
logging.config.fileConfig(abspath("logging.conf")) logging.config.fileConfig(abspath("logging.conf"))
@@ -76,6 +75,9 @@ except: # fails on GAE or when logfile is missing
logging.basicConfig() logging.basicConfig()
logger = logging.getLogger("web2py") logger = logging.getLogger("web2py")
exists = os.path.exists
pjoin = os.path.join
from gluon.restricted import RestrictedError from gluon.restricted import RestrictedError
from gluon.http import HTTP, redirect from gluon.http import HTTP, redirect
from gluon.globals import Request, Response, Session from gluon.globals import Request, Response, Session
@@ -99,17 +101,14 @@ requests = 0 # gc timer
try: try:
version_info = read_file(pjoin(global_settings.gluon_parent, 'VERSION')) version_info = read_file(pjoin(global_settings.gluon_parent, 'VERSION'))
raw_version_string = version_info.split()[-1].strip() web2py_version = global_settings.web2py_version = version_info.split()[-1].strip()
global_settings.web2py_version = raw_version_string
web2py_version = global_settings.web2py_version
except: except:
raise RuntimeError("Cannot determine web2py version") 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 from gluon import rocket
except:
if not global_settings.web2py_runtime_gae:
logger.warn('unable to import Rocket')
load_routes() load_routes()
+3
View File
@@ -62,6 +62,9 @@ if not os.path.isdir('applications'):
sys.path = [path] + [p for p in sys.path if not p == path] 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.main
wsgiref.handlers.CGIHandler().run(gluon.main.wsgibase) wsgiref.handlers.CGIHandler().run(gluon.main.wsgibase)
+3 -1
View File
@@ -40,6 +40,9 @@ if not os.path.isdir('applications'):
sys.path = [path] + [p for p in sys.path if not p == path] 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.main
import gluon.contrib.gateways.fcgi as fcgi import gluon.contrib.gateways.fcgi as fcgi
@@ -51,7 +54,6 @@ else:
application = gluon.main.wsgibase application = gluon.main.wsgibase
if SOFTCRON: if SOFTCRON:
from gluon.settings import global_settings
global_settings.web2py_crontype = 'soft' global_settings.web2py_crontype = 'soft'
fcgi.WSGIServer(application, bindAddress='/tmp/fcgi.sock').run() fcgi.WSGIServer(application, bindAddress='/tmp/fcgi.sock').run()
+5 -4
View File
@@ -49,14 +49,15 @@ from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext.webapp.util import run_wsgi_app
global_settings.web2py_runtime_handler = True
global_settings.web2py_runtime_gae = True global_settings.web2py_runtime_gae = True
global_settings.db_sessions = True global_settings.db_sessions = True
if os.environ.get('SERVER_SOFTWARE', '').startswith('Devel'): if os.environ.get('SERVER_SOFTWARE', '').startswith('Devel'):
(global_settings.web2py_runtime, DEBUG) = \ global_settings.web2py_runtime = 'gae:development'
('gae:development', True) DEBUG = True
else: else:
(global_settings.web2py_runtime, DEBUG) = \ global_settings.web2py_runtime = 'gae:production'
('gae:production', False) DEBUG = False
import gluon.main import gluon.main
+2
View File
@@ -13,6 +13,8 @@ def __ExtensionFactory__():
if not os.path.isdir('applications'): if not os.path.isdir('applications'):
raise RuntimeError('Running from the wrong folder') raise RuntimeError('Running from the wrong folder')
sys.path = [path] + [p for p in sys.path if not p == path] 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.main
import isapi_wsgi import isapi_wsgi
application = gluon.main.wsgibase application = gluon.main.wsgibase
+3
View File
@@ -40,6 +40,9 @@ if not os.path.isdir('applications'):
sys.path = [path] + [p for p in sys.path if not p == path] 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.main
+3 -1
View File
@@ -53,6 +53,9 @@ if not os.path.isdir('applications'):
sys.path = [path] + [p for p in sys.path if not p == path] 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.main
# uncomment one of the two imports below depending on the SCGIWSGI server installed # uncomment one of the two imports below depending on the SCGIWSGI server installed
@@ -70,7 +73,6 @@ else:
application = wsgiapp application = wsgiapp
if SOFTCRON: if SOFTCRON:
from gluon.settings import global_settings
global_settings.web2py_crontype = 'soft' global_settings.web2py_crontype = 'soft'
# uncomment one of the two rows below depending on the SCGIWSGI server installed # uncomment one of the two rows below depending on the SCGIWSGI server installed
+2
View File
@@ -21,6 +21,8 @@ from gevent import monkey
monkey.patch_all() monkey.patch_all()
def run(options): def run(options):
from gluon.settings import global_settings
global_settings.web2py_runtime_handler = True
import gluon.main import gluon.main
if options.password != '<recycle>': if options.password != '<recycle>':
gluon.main.save_password(options.password, int(options.port)) gluon.main.save_password(options.password, int(options.port))
+3 -1
View File
@@ -26,6 +26,9 @@ if not os.path.isdir('applications'):
sys.path = [path] + [p for p in sys.path if not p == path] 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.main
if LOGGING: if LOGGING:
@@ -36,5 +39,4 @@ else:
application = gluon.main.wsgibase application = gluon.main.wsgibase
if SOFTCRON: if SOFTCRON:
from gluon.settings import global_settings
global_settings.web2py_crontype = 'soft' global_settings.web2py_crontype = 'soft'