Flask、WSGI 和全局变量来保存数据库池
Flask, WSGI, and Global Variables to Hold Database Pool
我正在使用 WSGI/Apache2 并尝试在 init 上声明我的数据库池,以便从我的端点通过全局变量访问。我正在使用 Redis 和 Cassandra(特别是 DSE)。据我了解,Redis 和 DSE 库都提供池管理,因此这应该不是问题。
我的 WSGI 应用程序的文件夹结构类似于
folder/
tp.wsgi
app/
__init__.py
decorators/
cooldec.py
mod_api/
controllers.py
tp.wsgi
如下所示
#! /usr/bin/env python2.7
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/opt/tp")
from app import app
def application(environ, start_response):
return app(environ, start_response)
__init__.py
如下所示
#! /usr/bin/env python2.7
from flask import Flask
from cassandra.cluster import Cluster
# Import our handlers
from app.mod_api.files import mod_files
# Setup routine
def setup():
# Instantiate Flask
app = Flask('app')
# Set up a connection to Cassandra
cassandraSession = Cluster(['an ip address', 'an ip address']).connect('keyspace')
cassandraSession.default_timeout = None
# Register our blueprints
app.register_blueprint(mod_files)
...
return app, cassandraSession
app, cassandraSession = setup()
我正在调用 cooldec.py
中定义的处理身份验证的装饰器(出于某种原因,我松散地使用了该术语。我要求我们不要沿着使用 Flask 扩展进行身份验证的道路前进,那已经结束了这个问题的范围并且不适用于我的使用[参见:松散使用术语 'authentication'])
在 cooldec.py
和 controllers.py
中,我试图访问 cassandraSession
全局,但我一直在获取 global name 'cassandraSession' is not defined
。我知道错误的含义,但我不确定为什么会看到这个。据我了解,我设置 WSGI 应用程序的方式允许 cassandraSession
在应用程序范围内访问,不是吗?
我找到了 Preserving state in mod_wsgi Flask application 但是..它并没有真正说明我做错了什么。
我的问题是我的导入位置。我对 tp.wsgi
和 __init__.py
做了一些更改,我已经得到了我需要的工作。也就是说,从 cooldec.py
和 controllers.py
中调用 from app import cassandraSession
下面是我如何设置上述内容。
tp.wsgi
#! /usr/bin/env python2.7
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/opt/tp")
from app import app as application
__init__.py
#! /usr/bin/env python2.7
# API Module
from flask import Flask, jsonify
from cassandra.cluster import Cluster
# Create our API
app = Flask('app')
# Define a cassandra cluster/session we can use
cassandraSession = Cluster(['an ip address' 'an ip address']).connect('keyspace')
cassandraSession.default_timeout = None
... Register blueprints
这些是过于简化的编辑,但它给出了我做错了什么的想法(例如:在错误的文件中声明并尝试不正确地导入。
在cooldec.py
和controllres.py
中我们现在可以做
from app import cassandraSession
rows = cassandraSession.execute('select * from table')
给新 WSGI 开发者的提示:继续思考 "in python"。
+ 警告 +
关于这样做是否安全,我还没有找到绝对的答案。由于 sqlalchemy 处理连接池的方式,使用 sqlalchemy 执行此操作完全可以。到目前为止,我还不知道使用 Cassandra/DSE 是否安全,因此如果您使用此 post.
,请谨慎行事
我正在使用 WSGI/Apache2 并尝试在 init 上声明我的数据库池,以便从我的端点通过全局变量访问。我正在使用 Redis 和 Cassandra(特别是 DSE)。据我了解,Redis 和 DSE 库都提供池管理,因此这应该不是问题。
我的 WSGI 应用程序的文件夹结构类似于
folder/
tp.wsgi
app/
__init__.py
decorators/
cooldec.py
mod_api/
controllers.py
tp.wsgi
如下所示
#! /usr/bin/env python2.7
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/opt/tp")
from app import app
def application(environ, start_response):
return app(environ, start_response)
__init__.py
如下所示
#! /usr/bin/env python2.7
from flask import Flask
from cassandra.cluster import Cluster
# Import our handlers
from app.mod_api.files import mod_files
# Setup routine
def setup():
# Instantiate Flask
app = Flask('app')
# Set up a connection to Cassandra
cassandraSession = Cluster(['an ip address', 'an ip address']).connect('keyspace')
cassandraSession.default_timeout = None
# Register our blueprints
app.register_blueprint(mod_files)
...
return app, cassandraSession
app, cassandraSession = setup()
我正在调用 cooldec.py
中定义的处理身份验证的装饰器(出于某种原因,我松散地使用了该术语。我要求我们不要沿着使用 Flask 扩展进行身份验证的道路前进,那已经结束了这个问题的范围并且不适用于我的使用[参见:松散使用术语 'authentication'])
在 cooldec.py
和 controllers.py
中,我试图访问 cassandraSession
全局,但我一直在获取 global name 'cassandraSession' is not defined
。我知道错误的含义,但我不确定为什么会看到这个。据我了解,我设置 WSGI 应用程序的方式允许 cassandraSession
在应用程序范围内访问,不是吗?
我找到了 Preserving state in mod_wsgi Flask application 但是..它并没有真正说明我做错了什么。
我的问题是我的导入位置。我对 tp.wsgi
和 __init__.py
做了一些更改,我已经得到了我需要的工作。也就是说,从 cooldec.py
和 controllers.py
from app import cassandraSession
下面是我如何设置上述内容。
tp.wsgi
#! /usr/bin/env python2.7
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/opt/tp")
from app import app as application
__init__.py
#! /usr/bin/env python2.7
# API Module
from flask import Flask, jsonify
from cassandra.cluster import Cluster
# Create our API
app = Flask('app')
# Define a cassandra cluster/session we can use
cassandraSession = Cluster(['an ip address' 'an ip address']).connect('keyspace')
cassandraSession.default_timeout = None
... Register blueprints
这些是过于简化的编辑,但它给出了我做错了什么的想法(例如:在错误的文件中声明并尝试不正确地导入。
在cooldec.py
和controllres.py
中我们现在可以做
from app import cassandraSession
rows = cassandraSession.execute('select * from table')
给新 WSGI 开发者的提示:继续思考 "in python"。
+ 警告 +
关于这样做是否安全,我还没有找到绝对的答案。由于 sqlalchemy 处理连接池的方式,使用 sqlalchemy 执行此操作完全可以。到目前为止,我还不知道使用 Cassandra/DSE 是否安全,因此如果您使用此 post.
,请谨慎行事