【发布时间】:2017-09-12 10:08:01
【问题描述】:
app_instance.py
from app import FlaskApp
app = None
def init_instance(env):
global app
app = FlaskApp(env)
def get_instance():
assert app is not None
return app
FlaskApp class 差不多是这样的
class FlaskApp(object):
def __init__(self, env):
self.oauth_manager = .... bla bla ..
self.clients_manager = .. bla bla ..
app = Flask(__name__)
app.config.from_object(env)
app = app_wrapper.wrap(app, app.config['NUM_PROXY_SERVERS'])
self.app = app
self.api = Api(self.app, prefix='/v3', default_mediatype='application/json')
self.define_routes()
# Initialize the DB
self.db = Database(self.app)
fmt = "%(asctime)s - %(request_id)s - %(name)s - %(levelname)s - %(message)s"
logging.basicConfig(format=fmt, level=self.app.config.get('LOG_LEVEL'))
request_id.init(app, prefix='MY_API_', internal=False)
def run_server(self):
self.app.run(host=self.app.config['HOST'], port=self.app.config['PORT'], debug=self.app.config['DEBUG'])
def define_routes(self):
# Configure Api Resources
self.api.add_resource(VersionListController, '/my/route', endpoint='versions')
more routes here
self.api.init_app(self.app)
在我的应用控制器中
def is_valid_oauth_token(request):
from mobile_module import app_instance
app = app_instance.get_instance()
# more code here
我正在本地主机上运行应用程序并获取
assert app is not None
AssertionError
如何“修复”此代码?我应该在每个路由访问中导入from mobile_module import app_instance 吗?请提出建议
我应该声明这个应用程序在 Nginx 之后的生产环境中运行良好
我想我的问题更多是关于 python(如何使这项工作)而不是烧瓶。
【问题讨论】:
-
您是否尝试使用app context?
from flask import current_app... -
你能给我们FlaskApp类的代码吗??
-
@EspoirMurhabazi 添加到问题中
-
好的,!但是您如何看待在我的回答中使用应用工厂设计模式 ass 建议?
-
@DanilaGanchar app_context 成功了。请把你的答案写成答案,我会批准
标签: python python-2.7 flask