Skip to content

Instantly share code, notes, and snippets.

@iromli
Last active December 20, 2015 04:59
Show Gist options
  • Save iromli/6074491 to your computer and use it in GitHub Desktop.
Save iromli/6074491 to your computer and use it in GitHub Desktop.
import os
from flask import Flask
from flask.ext.script import Manager
from flask.ext.script import Command
from gunicorn.app.base import Application
FOOBAR = True
def create_app(config=None):
app = Flask(
__name__,
instance_path=os.getcwd(),
instance_relative_config=True,
)
app.config.from_object(__name__)
if config:
app.config.from_pyfile(config)
return app
manager = Manager(create_app)
manager.add_option('-c', dest='config', required=False)
class Gunicorn(script.Command):
def handle(self, app, *args, **kwargs):
class GWSGIApp(Application):
def init(self, parser, opts, args):
globals, locals = {}, {}
if opts.config:
execfile(opts.config, globals, locals)
cfg = {}
for k, v in locals.iteritems():
gunicorn_key = k.lstrip('GUNICORN_').lower()
if gunicorn_key in self.cfg.settings and v is not None:
cfg[gunicorn_key] = v
return cfg
def load(self):
return app
GWSGIApp().run()
def main():
manager.add_command('run-gunicorn', Gunicorn())
manager.run()
if __name__ == '__main__':
main()
FOOBAR = False
GUNICORN_WORKER_CLASS = 'gevent'
GUNICORN_WORKERS = 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment