mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
commit changes from @davisp + some fixes
This commit is contained in:
parent
f7c91e7e86
commit
62540cffaa
@ -21,6 +21,7 @@ class Application(object):
|
|||||||
def __init__(self, usage=None):
|
def __init__(self, usage=None):
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
self.cfg = Config(usage)
|
self.cfg = Config(usage)
|
||||||
|
self.callable = None
|
||||||
|
|
||||||
parser = self.cfg.parser()
|
parser = self.cfg.parser()
|
||||||
opts, args = parser.parse_args()
|
opts, args = parser.parse_args()
|
||||||
@ -67,6 +68,11 @@ class Application(object):
|
|||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def wsgi(self):
|
||||||
|
if self.callable is None:
|
||||||
|
self.callable = self.load()
|
||||||
|
return self.callable
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.cfg.spew:
|
if self.cfg.spew:
|
||||||
|
|||||||
@ -51,6 +51,8 @@ class DjangoApplicationCommand(Application):
|
|||||||
|
|
||||||
def __init__(self, options, admin_media_path):
|
def __init__(self, options, admin_media_path):
|
||||||
self.cfg = Config()
|
self.cfg = Config()
|
||||||
|
self.callable = None
|
||||||
|
|
||||||
for k, v in list(options.items()):
|
for k, v in list(options.items()):
|
||||||
if k.lower() in self.cfg.settings and v is not None:
|
if k.lower() in self.cfg.settings and v is not None:
|
||||||
self.cfg.set(k.lower(), v)
|
self.cfg.set(k.lower(), v)
|
||||||
|
|||||||
@ -63,6 +63,7 @@ class PasterServerApplication(Application):
|
|||||||
def __init__(self, app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
|
def __init__(self, app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
|
||||||
self.cfg = Config()
|
self.cfg = Config()
|
||||||
self.app = app
|
self.app = app
|
||||||
|
self.callable = None
|
||||||
|
|
||||||
cfg = kwargs.copy()
|
cfg = kwargs.copy()
|
||||||
|
|
||||||
|
|||||||
@ -45,12 +45,11 @@ class Arbiter(object):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
|
self.app = app
|
||||||
self.cfg = app.cfg
|
self.cfg = app.cfg
|
||||||
|
|
||||||
if self.cfg.preload_app:
|
if self.cfg.preload_app:
|
||||||
self.app = app.load()
|
self.app.wsgi()
|
||||||
else:
|
|
||||||
self.app = app
|
|
||||||
|
|
||||||
self.log = logging.getLogger(__name__)
|
self.log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@ -349,6 +349,21 @@ with Setting("spew") as s:
|
|||||||
This is the nuclear option.
|
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:
|
with Setting("daemon") as s:
|
||||||
s.section = "Server Mechanics"
|
s.section = "Server Mechanics"
|
||||||
s.cli = ["-D", "--daemon"]
|
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.
|
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.
|
|
||||||
""")
|
|
||||||
@ -57,7 +57,7 @@ class AsyncWorker(Worker):
|
|||||||
environ = req.read()
|
environ = req.read()
|
||||||
if not environ or not req.parser.headers:
|
if not environ or not req.parser.headers:
|
||||||
return False
|
return False
|
||||||
respiter = self.app(environ, req.start_response)
|
respiter = self.wsgi(environ, req.start_response)
|
||||||
if respiter == ALREADY_HANDLED:
|
if respiter == ALREADY_HANDLED:
|
||||||
return False
|
return False
|
||||||
for item in respiter:
|
for item in respiter:
|
||||||
|
|||||||
@ -91,9 +91,7 @@ class Worker(object):
|
|||||||
util.close_on_exec(self.fd)
|
util.close_on_exec(self.fd)
|
||||||
self.init_signals()
|
self.init_signals()
|
||||||
|
|
||||||
# do we need to load the app
|
self.wsgi = self.app.wsgi()
|
||||||
if not self.cfg.preload_app:
|
|
||||||
self.app = self.app.load()
|
|
||||||
|
|
||||||
# Enter main run loop
|
# Enter main run loop
|
||||||
self.run()
|
self.run()
|
||||||
|
|||||||
@ -50,9 +50,9 @@ class TornadoWorker(Worker):
|
|||||||
# Assume the app is a WSGI callable if its not an
|
# Assume the app is a WSGI callable if its not an
|
||||||
# instance of tornardo.web.Application
|
# instance of tornardo.web.Application
|
||||||
if not isinstance(self.app, tornado.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._socket = self.socket
|
||||||
server.start(num_processes=1)
|
server.start(num_processes=1)
|
||||||
|
|
||||||
|
|||||||
@ -94,7 +94,7 @@ class SyncWorker(Worker):
|
|||||||
environ = req.read()
|
environ = req.read()
|
||||||
if not environ or not req.parser.status_line:
|
if not environ or not req.parser.status_line:
|
||||||
return
|
return
|
||||||
respiter = self.app(environ, req.start_response)
|
respiter = self.wsgi(environ, req.start_response)
|
||||||
for item in respiter:
|
for item in respiter:
|
||||||
req.response.write(item)
|
req.response.write(item)
|
||||||
req.response.close()
|
req.response.close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user