问题描述
我正在使用应用程序工厂方法创建一个flask应用程序,但是在将Flask-Migrate与socketio和flask-script一起使用时遇到问题.
I'm creating an flask application with the application factory approach, but have an issue when using Flask-Migrate with socketio and flask-script.
问题是我要将create_app
函数传递给Manager
,但是我也需要将app
传递给我的socketio.run()
.现在,我似乎看不到解决方案.有什么办法可以将这两种解决方案结合起来?
The problem is that I'm passing my create_app
function to the Manager
but I need to pass the app
to my socketio.run()
as well. And right now I can't seem to see a solution. Is there any way I can combine these two solutions?
manage.py:
manage.py:
#app = create_app(False) <--- Old approach
#manager = flask_script.Manager(app)
manager = flask_script.Manager(create_app)
manager.add_option("-t", "--testing", dest="testing", required=False)
manager.add_command("run", socketio.run(
app,
host='127.0.0.1',
port=5000,
use_reloader=False)
)
# DB Management
manager.add_command("db", flask_migrate.MigrateCommand)
当我将旧方法与socketio一起使用时,如果不进行烧瓶迁移,一切都将起作用.如果我使用新方法,并删除了socketio部分,则迁移工作正常.
When I used the old approach with socketio, but without flask-migrate everything worked. If I use the new approach, and remove the socketio part, the migrate works.
注意:我希望能够使用以下两个命令来调用我的应用程序.python manage.py run
python manage.py -t True db upgrade
Note: I would like to be able to call my app with both of the following commands.python manage.py run
python manage.py -t True db upgrade
尝试使用current_app
我正在获取RuntimeError: working outside of application context
manager.add_command("run", socketio.run(
flask.current_app,
host='127.0.0.1',
port=5000,
use_reloader=False)
)
推荐答案
基于Miguel的评论,我找到了一种可行的方法.
Based on the comment by Miguel I found a way which works.
由于某些原因,以下代码不起作用
For some reason the following code does not work
manager.add_command("run", socketio.run(
flask.current_app,
host='127.0.0.1',
port=5000,
use_reloader=False)
)
但这确实有效.
@manager.command
def run():
socketio.run(flask.current_app,
host='127.0.0.1',
port=5000,
use_reloader=False)
这篇关于使用flask-migrate和flask-script,flask-socketio和应用程序工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!