Browse Source

Use a global cached server object for WSGI app

This is instead of caching it inside nilmdb.server.wsgi_application.
Might make things work a bit better in case the web server decides
to call wsgi_application multiple times.
tags/nilmdb-1.4.5^0
Jim Paris 11 years ago
parent
commit
b5fefffa09
1 changed files with 10 additions and 5 deletions
  1. +10
    -5
      nilmdb/server/server.py

+ 10
- 5
nilmdb/server/server.py View File

@@ -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

Loading…
Cancel
Save