commit changes from @davisp + some fixes

This commit is contained in:
benoitc 2010-05-22 21:11:25 +02:00
parent f7c91e7e86
commit 62540cffaa
9 changed files with 31 additions and 20 deletions

View File

@ -21,6 +21,7 @@ class Application(object):
def __init__(self, usage=None):
self.log = logging.getLogger(__name__)
self.cfg = Config(usage)
self.callable = None
parser = self.cfg.parser()
opts, args = parser.parse_args()
@ -67,6 +68,11 @@ class Application(object):
def load(self):
raise NotImplementedError
def wsgi(self):
if self.callable is None:
self.callable = self.load()
return self.callable
def run(self):
if self.cfg.spew:

View File

@ -51,6 +51,8 @@ class DjangoApplicationCommand(Application):
def __init__(self, options, admin_media_path):
self.cfg = Config()
self.callable = None
for k, v in list(options.items()):
if k.lower() in self.cfg.settings and v is not None:
self.cfg.set(k.lower(), v)

View File

@ -63,6 +63,7 @@ class PasterServerApplication(Application):
def __init__(self, app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
self.cfg = Config()
self.app = app
self.callable = None
cfg = kwargs.copy()

View File

@ -45,12 +45,11 @@ class Arbiter(object):
)
def __init__(self, app):
self.app = app
self.cfg = app.cfg
if self.cfg.preload_app:
self.app = app.load()
else:
self.app = app
self.app.wsgi()
self.log = logging.getLogger(__name__)

View File

@ -349,6 +349,21 @@ with Setting("spew") as s:
This is the nuclear option.
""")
with Setting("preload_app") as s:
s.section = "Server Mechanics"
s.cli = ["--preload"]
s.validator = validate_bool
s.action = "store_true"
s.default = False
s.fmt_desc("""\
Load application code before the worker processes are forked.
By preloading an application you can save some RAM resources as well as
speed up server boot times. Although, if you defer application loading
to each worker process, you can reload your application code easily by
restarting workers.
""")
with Setting("daemon") as s:
s.section = "Server Mechanics"
s.cli = ["-D", "--daemon"]
@ -522,13 +537,3 @@ with Setting("pre_exec") as s:
The callable needs to accept a single instance variable for the Arbiter.
""")
with Setting("preload_app") as s:
s.section = "Server Mechanic"
s.cli = ["--preload"]
s.validator = validate_bool
s.action = "store_true"
s.default = False
s.fmt_desc("""\
Enabling this preloads an application before forking worker processes.
""")

View File

@ -57,7 +57,7 @@ class AsyncWorker(Worker):
environ = req.read()
if not environ or not req.parser.headers:
return False
respiter = self.app(environ, req.start_response)
respiter = self.wsgi(environ, req.start_response)
if respiter == ALREADY_HANDLED:
return False
for item in respiter:

View File

@ -91,9 +91,7 @@ class Worker(object):
util.close_on_exec(self.fd)
self.init_signals()
# do we need to load the app
if not self.cfg.preload_app:
self.app = self.app.load()
self.wsgi = self.app.wsgi()
# Enter main run loop
self.run()

View File

@ -50,9 +50,9 @@ class TornadoWorker(Worker):
# Assume the app is a WSGI callable if its not an
# instance of tornardo.web.Application
if not isinstance(self.app, tornado.web.Application):
self.app = WSGIContainer(self.app)
self.app = WSGIContainer(self.wsgi)
server = HTTPServer(self.app, io_loop=self.ioloop)
server = HTTPServer(self.wsgi, io_loop=self.ioloop)
server._socket = self.socket
server.start(num_processes=1)

View File

@ -94,7 +94,7 @@ class SyncWorker(Worker):
environ = req.read()
if not environ or not req.parser.status_line:
return
respiter = self.app(environ, req.start_response)
respiter = self.wsgi(environ, req.start_response)
for item in respiter:
req.response.write(item)
req.response.close()