@@ -609,6 +609,11 @@ class Server(object):
def stop(self):
cherrypy.engine.exit()
# Use a single global nilmdb.server.NilmDB and nilmdb.server.Server
# instance since the database can only be opened once. For this to
# work, the web server must use only a single process and single
# Python interpreter. Multiple threads are OK.
_wsgi_server = None
def wsgi_application(dbpath, basepath): # pragma: no cover
"""Return a WSGI application object with a database at the
specified path.
@@ -619,13 +624,13 @@ def wsgi_application(dbpath, basepath): # pragma: no cover
is the same as the first argument to Apache's WSGIScriptAlias
directive.
"""
server = [None]
def application(environ, start_response):
if server[0] is None:
global _wsgi_server
if _wsgi_server is None:
# Try to start the server
try:
db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(dbpath)
server[0] = nilmdb.server.Server(
_wsgi_ server = nilmdb.server.Server(
db, embedded = True,
basepath = basepath.rstrip('/'))
except Exception:
@@ -645,7 +650,7 @@ def wsgi_application(dbpath, basepath): # pragma: no cover
except ImportError:
pass
err += sprintf("\nEnvironment:\n%s\n", pprint.pformat(environ))
if server[0] is None:
if _wsgi_ server is None:
# Serve up the error with our own mini WSGI app.
headers = [ ('Content-type', 'text/plain'),
('Content-length', str(len(err))) ]
@@ -653,5 +658,5 @@ def wsgi_application(dbpath, basepath): # pragma: no cover
return [err]
# Call the normal application
return server[0] .wsgi_application(environ, start_response)
return _wsgi_ server.wsgi_application(environ, start_response)
return application