简体   繁体   English

有没有更好的方法来获取flask的app_context?

[英]Is there any better way to get flask's app_context?

In Flask, I start another thread to run a time consuming task which need to update mysql, how could I use Flask_sqlalchemy in this thread? 在Flask中,我启动了另一个线程来运行耗时的任务,该任务需要更新mysql,如何在该线程中使用Flask_sqlalchemy?

I try to run a bash script when request a url in my website. 在我的网站中请求URL时,我尝试运行bash脚本。 Because the bash script need long time to run, I don't want it block the flask's main thread . 因为bash脚本需要很长时间才能运行,所以我不希望它阻塞烧瓶的主线程 So I start another thread to run it. 因此,我启动了另一个线程来运行它。 When I did this, I find that I need to get flask's app_context to use Flask_sqlalchemy . 完成此操作后,我发现需要获取flask的app_context才能使用Flask_sqlalchemy So I did like this: 所以我确实是这样的:

part of my code: 我的代码的一部分:

@manage_git.route('/makefile', methods=['POST'])
def makefile():
    Threading(target=mkfile, args=(local_git_dir, git_id)).start()
    return redirect(url_for('manage_git.index'))

def mkfile(local_git_dir, git_id):
    from manage import app
    subprocess.Popen(['bash', 'makefile.sh'])
    with app.app_context():
        git_db = GitDb.query.filter_by(id=git_id).first()
        git_db.make = True
        db.session.add(git_db)
        db.session.commit()

I just want app_context to use Flask_sqlalchemy, but I import a Global Variable app . 我只希望app_context使用Flask_sqlalchemy,但是我导入了Global Variable app I don't think it is a good way to solve this problem. 我认为这不是解决此问题的好方法。

It would help me a lot, If you could tell me about: Is there any better way to get flask's app_context? 如果您能告诉我以下信息,这对我会有很大帮助: 是否有更好的方法来获取flask的app_context? Or is there another better way to solve my problem? 还是有其他更好的方法来解决我的问题?

----supplementary---- - - 补充 - -

exts.py : exts.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

init .py : 初始化 .py

from flask import FLask

from config import config

from .exts import db, login_manager


def create_app(config_name):
    app = Flask(__name__)
    app_config = config[config_name]
    app.config.from_object(app_config)
    app_config.init_app(app)

    db.init_app(app)

    from .manage_git import manage_git as manage_git_blueprint
    app.register_blueprint(manage_git_blueprint)
    # ......
    return app

manage.py : manage.py

from app import create_app

from app.models import *

app = create_app('default')

if __name__ == '__main__':
    app.run()

How are you creating your app and db objects? 您如何创建应用程序和数据库对象? According to the Flask-SQLAlchemy docs, if you are only using on App object, you can just pass the app object to your SQLAlchemy() call: 根据Flask-SQLAlchemy文档,如果仅在App对象上使用,则可以将app对象传递给SQLAlchemy()调用:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)

Now when you import the db, you would not have a need to import the app or using the app_context: 现在,当您导入数据库时​​,就不需要导入应用程序或使用app_context了:

def mkfile(local_git_dir, git_id):
    subprocess.Popen(['bash', 'makefile.sh'])
    git_db = GitDb.query.filter_by(id=git_id).first()
    git_db.make = True
    db.session.add(git_db)
    db.session.commit()

http://flask-sqlalchemy.pocoo.org/2.3/contexts/ and http://flask-sqlalchemy.pocoo.org/2.3/quickstart/ have more information about this process and the alternatives. http://flask-sqlalchemy.pocoo.org/2.3/contexts/http://flask-sqlalchemy.pocoo.org/2.3/quickstart/具有有关此过程和替代方法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM