Skip to content

Instantly share code, notes, and snippets.

@hktonylee hktonylee/manage.py
Last active Dec 19, 2018

Embed
What would you like to do?
Flask + manage.py + gunicorn + auto-reload + Flask-Sockets + anything you can do with gunicorn script
#!./venv/bin/python3
import os
import sys
import subprocess
from flask_migrate import MigrateCommand
from flask_script import Manager, Command, Option
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(BASE_DIR, 'src'))
# Originally from https://gist.github.com/menghan/9a632bbb0acb445f4f3a
class GunicornServer(Command):
"""Run the app within Gunicorn"""
def get_options(self):
from gunicorn.config import make_settings
settings = make_settings()
options = []
for setting, klass in settings.items():
if klass.cli:
if klass.const is not None:
options.append(Option(*klass.cli, const=klass.const, action=klass.action))
else:
options.append(Option(*klass.cli, action=klass.action))
return options
def run(self, *args, **kwargs):
run_args = sys.argv[2:]
# !!!!! Change your app here !!!!!
run_args.append('--reload')
run_args.extend(['-k', 'flask_sockets.worker'])
run_args.append('server.gui_server:app')
subprocess.Popen([os.path.join(os.path.dirname(sys.executable), 'gunicorn')] + run_args).wait()
# !!!!! Change your app here !!!!!
from server.gui_server import app
manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.add_command('runserver', GunicornServer)
if __name__ == '__main__':
manager.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.