Reputation: 151
I have working flask app on my development server of flask which works fine. Now i want to take it one step further to deploy it using the gunicorn i have following code in which i can launch gunicorn but my application some where in middle drop connection but it works very fine DEV server.
I would like to know how to enable logging on with gunicorn.
I review the following que but could not get much information How to use Flask-Script and Gunicorn
My Application has following structure and /home/webusr/svsapp/svsappenv
manage.py has following code updated my manage.py with respect to following blog post
#!/usr/bin/env python
import os
import sys
from gunicorn.app.base import Application
from app import create_app,db
from flask.ext.script import Manager, Shell , Server
from flask.ext.migrate import Migrate, MigrateCommand
from flask_script import Command,Option
from app.models import SVSFaceTab,SVSuserReg,SVSIpCamReg
app = create_app(os.getenv('SVS_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db,SVSuserReg=SVSuserReg,SVSIpCamReg=SVSIpCamReg,SVSFaceTab=SVSFaceTab)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
@manager.option('-h', '--host', dest='host', default='169.38.74.171')
@manager.option('-p', '--port', dest='port', type=int, default=8080)
@manager.option('-w', '--workers', dest='workers', type=int, default=10)
@manager.option('-t', '--timeout', dest='timeout', type=int ,default=90)
def gunicorn(host, port, workers,timeout):
"""Start the Server with Gunicorn"""
from gunicorn.app.base import Application
class FlaskApplication(Application):
def init(self, parser, opts, args):
return {
'bind': '{0}:{1}'.format(host, port),
'workers': workers,'timeout' : timeout
}
def load(self):
return app
application = FlaskApplication()
return application.run()
@manager.command
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if __name__ == '__main__':
manager.run()
$ python manage.py gunicorn
Upvotes: 2
Views: 2386
Reputation: 115
I am using this solution based on https://bitbucket.org/youngking/flask-actions/src/381d6eea3e78/flaskext/actions/server_actions.py?fileviewer=file-view-default#cl-81
class GunicornServer(Command):
description = 'to run the app within Gunicorn'
def __init__(self, host='0.0.0.0', port=5000, workers=2):
self.port = port
self.host = host
self.workers = workers
def get_options(self):
return (
Option('-H', '--host',
dest='host',
default=self.host),
Option('-p', '--port',
dest='port',
type=int,
default=self.port),
Option('-w', '--workers',
dest='workers',
type=int,
default=self.workers),
)
def handle(self, app, host, port, workers):
from gunicorn import version_info
if version_info < (0, 9, 0):
from gunicorn.arbiter import Arbiter
from gunicorn.config import Config
arbiter = Arbiter(
Config(
{'bind': "%s:%d" % (host, int(port)), 'workers': workers}
),
app
)
arbiter.run()
else:
from gunicorn.app.base import Application
class FlaskApplication(Application):
def init(self, parser, opts, args):
return {
'bind': '{0}:{1}'.format(host, port),
'workers': workers
}
def load(self):
return app
FlaskApplication().run()
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
# Adding gunicorn based runserver command
manager.add_command("gunicorn", GunicornServer())
I am running the app like:
./manage.py gunicorn
Notice the last line manage.add_command, if you want you can just override "runserver" with GunicornServer() class.
Upvotes: 1