2017-04-22 23 views

回答

2

说明

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

要看到两种行为,手动改变不良=假坏=真。

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 

参考

相关问题