2015-12-14 333 views
1

我有我的燒瓶應用程序在我的開發服務器的燒瓶工作正常。現在我想進一步使用gunicorn來部署它,我有以下代碼,我可以在其中啓動gunicorn,但我的應用程序在中間的下拉連接處有一些地方,但它的工作非常好,DEV服務器。在燒瓶腳本上燒瓶部署燒瓶腳本

我想知道如何啓用gunicorn登錄。

我檢查以下闕,但不能得到太多的信息 How to use Flask-Script and Gunicorn

我的應用程序具有以下結構和 /家庭/ webusr/svsapp/svsappenv

manage.py有以下代碼更新了我manage.py關於以下博文

#!/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 
+1

'gunicorn管理:應用程序? (http://flask.pocoo.org/docs/0.10/deploying/wsgi-standalone/#gunicorn) – wgwz

+0

請將其添加到OP並對其進行格式化,以便閱讀。 – wgwz

+0

謝謝我會這樣做 – Nic

回答

0

我使用該解決方案基於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,如果你願意,你可以只覆蓋「 runserver「與GunicornServer()類。