Created
January 22, 2019 02:40
-
-
Save jmvrbanac/884331af1385c767f7ecb8b34b261291 to your computer and use it in GitHub Desktop.
Custom Gunicorn App that supports both meinheld and sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gunicorn.app.base import BaseApplication | |
from gunicorn.workers.sync import SyncWorker | |
from meinheld.gmeinheld import MeinheldWorker | |
class CustomWorker(object): | |
def handle_quit(self, sig, frame): | |
self.app.application.stop(sig) | |
super().handle_quit(sig, frame) | |
def run(self): | |
self.app.application.start() | |
try: | |
super().run() | |
except KeyboardInterrupt: | |
pass | |
class CustomMeinheldWorker(CustomWorker, MeinheldWorker): | |
pass | |
class CustomSyncWorker(CustomWorker, SyncWorker): | |
pass | |
class GunicornApp(BaseApplication): | |
def __init__(self, app, options=None): | |
self.options = options or {} | |
self.application = app | |
super(GunicornApp, self).__init__() | |
def load_config(self): | |
self.cfg.set('worker_class', 'myapp.__main__.CustomMeinheldWorker') | |
for key, value in self.options.items(): | |
self.cfg.set(key.lower(), value) | |
def init(self, parser, opts, args): | |
pass | |
def load(self): | |
return self.application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment