0
我有一個連接到Cassandra的Flask應用程序。當我在Gunicorn下運行這個應用程序並將Gunicorn作爲高腳本命令python manage.py gunicorn
時,它會掛起。但是當我在命令行上運行這個相同的應用程序gunicorn manage:app
,它會成功。爲什麼?當我通過Gunicorn和Flask連接到Cassandra時,它爲什麼會懸掛?
我有一個連接到Cassandra的Flask應用程序。當我在Gunicorn下運行這個應用程序並將Gunicorn作爲高腳本命令python manage.py gunicorn
時,它會掛起。但是當我在命令行上運行這個相同的應用程序gunicorn manage:app
,它會成功。爲什麼?當我通過Gunicorn和Flask連接到Cassandra時,它爲什麼會懸掛?
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