当我通过Gunicorn和Flask连接到Cassandra时,为什么会挂?

时间:2017-04-22 01:48:24

标签: flask cassandra gunicorn flask-script

我有一个连接到Cassandra的Flask应用程序。当我在Gunicorn下运行这个应用程序并调用Gunicorn作为flask-script命令python manage.py gunicorn时,它会挂起。但是当我在命令行上以gunicorn manage:app运行同一个应用程序时,它会成功。为什么呢?

1 个答案:

答案 0 :(得分:3)

解释

Gunicorn要求工人处理传入的请求。 如果在之前创建了Cassandra会话(连接池)(例如,在使用应用程序工厂模式创建应用程序期间),则工作人员将使用Cassandra时遇到问题。 DataStax建议每个工作人员都有自己的会话,因此您需要推迟创建会话,直到之后。 当你将Gunicorn和Flask捆绑在一起作为自定义应用程序时,这是一个问题,但可能在命令行上,Gunicorn显然可以在创建Flask应用程序之前初始化并完全分叉。

实施例

要查看这两种行为,请手动将bad = False更改为bad = True。

from cassandra.cluster import Cluster
from flask import Flask
from flask_script import Command, Manager
from gunicorn.app.base import BaseApplication
from gunicorn.six import iteritems


class CassandraClient:
    def __init__(self, bad=False):
        self.session = None
        self.cluster = None
        if bad:
            self.connect()

    def connect(self):
        self.cluster = Cluster(['127.0.0.1'])
        self.session = self.cluster.connect('keyspace')

    def execute(self):
        if not self.session:
            self.connect()
        query = '''
            select now()
            from system.local
        '''
        return self.session.execute(query)[0]


class GunicornApp(BaseApplication):
    '''
    Bundle Gunicorn and Flask together, so that we can call it as a
    flask-script command.

    http://docs.gunicorn.org/en/stable/custom.html
    '''

    def __init__(self, app, options=None):
        self.options = options or {}
        self.application = app
        super(GunicornApp, self).__init__()

    def load_config(self):
        config = dict(
            [(key, value) for key, value in iteritems(self.options)
             if key in self.cfg.settings and value is not None])
        for key, value in iteritems(config):
            self.cfg.set(key.lower(), value)

    def load(self):
        return self.application


class GunicornCommand(Command):
    '''
    Modeled off of flask_script.Server
    '''
    def __init__(self, app, options):
        self.app = app
        self.options = options

    def __call__(self, *args, **kwargs):
        GunicornApp(self.app, self.options).run()


app = Flask(__name__)
app.cassandra = CassandraClient()
@app.route('/')
def hello():
    return str(app.cassandra.execute())


if __name__ == '__main__':
    manager = Manager(app)
    gunicorn_options = {
        'bind': '127.0.0.1',
        'port': 8000,
        'workers': 4
    }
    manager.add_command("gunicorn", GunicornCommand(app, gunicorn_options))
    manager.run()

版本

Flask==0.12.1  
Flask-Script==2.0.5  
gunicorn==19.7.1  
cassandra-driver==3.8.1 

参考