在烧瓶脚本上的烧瓶部署

时间:2015-12-14 11:25:51

标签: python flask gunicorn

我在我的开发服务器上工作烧瓶应用程序工作正常。现在我想进一步使用gunicorn部署它我有以下代码,我可以启动gunicorn但我的应用程序在中间连接的一些地方,但它工作非常精美的DEV服务器。

我想知道如何使用gunicorn启用登录。

我回顾了以下问题,但无法获得更多信息 How to use Flask-Script and Gunicorn

我的应用程序具有以下结构和 /家庭/ webusr / svsapp / svsappenv

manage.py 以下代码更新了我的manage.py以及以下博文

using (StreamReader reader = new StreamReader(fileName))
    {
        string line; 

        while ((line = reader.ReadLine()) != null)
        {
            //Define pattern
            Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

            //Separating columns to array
            string[] X = CSVParser.Split(line);

            /* Do something with X */
        }
    }
#!/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()

1 个答案:

答案 0 :(得分:1)

我正在使用基于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())

我正在运行应用程序,如:

./manage.py gunicorn

注意最后一行manage.add_command,如果你想要你可以用GunicornServer()类覆盖“runserver”。