Flask_Sqlalchemy与多线程Apache.会话与数据库不同步 [英] Flask_Sqlalchemy with multithreaded Apache. Sessions out of sync with database

查看:289
本文介绍了Flask_Sqlalchemy与多线程Apache.会话与数据库不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:Apache服务器使用mod_wsgi通过Flask_Sqlalchemy连接到MySQL为Flask应用提供服务.这是一个全栈应用程序,因此几乎不可能创建一个最小的示例,但是我已经尝试过了.

Background: Apache server using mod_wsgi to serve a Flask app using Flask_Sqlalchemy connecting to MySQL. This is a full stack application so it is nearly impossible to create a minimal example but I have tried.

我的问题是,当我进行应修改数据库的某些更改时,后续请求似乎并不总是反映出该更改.例如,如果我创建一个对象,然后尝试编辑该对象,则编辑有时会失败.

My problem is that when I make some change that should modify the database subsequent requests don't always seem to reflect that change. For example if I create an object, then try to edit that same object, the edit will sometimes fail.

大多数时候,如果我创建一个对象,然后转到列出所有对象的页面,它将不会显示在列表中.有时,它会一直显示直到我刷新为止,然后消失,然后再次显示它.

Most of the time if I create an object then go to the page listing all the objects, it will not show up on the list. Sometimes it will show up until I refresh, when it will disappear, and with another refresh it shows up again.

编辑也会发生同样的情况.示例代码:

The same happens with edits. Example code:

bp = Blueprint('api_region', __name__, url_prefix='/app/region')
@bp.route('/rename/<int:region_id>/<string:name>', methods=['POST'])
def change_name(region_id, name):
    region = Region.query.get(region_id)
    try:
        region.name = name
    except AttributeError:
        abort(404)
    db.session.add(region)
    db.session.commit()
    return "Success"


@bp.route('/name/<int:region_id>/', methods=['GET'])
def get_name(region_id):
    region = Region.query.get(region_id)
    try:
        name = region.name
    except AttributeError:
        abort(404)

    return name

创建对象后,发送POST

After object is created send a POST

curl -X POST https://example.com/app/region/rename/5/Europe

然后是几个GET

curl -X GET https://example.com/app/region/name/5/

有时,GET将返回正确的信息,但是有时会返回以前的信息.进一步的示例输出 https://pastebin.com/s8mqRHSR 它以变化的频率发生,但大约25分之一会失败,它也不总是最后一个"值,在测试时,无论我将其更改多少次,它似乎都卡在"某个值上.

Sometimes, the GET will return the correct info, but every now and then it will return whatever it was before. Further example output https://pastebin.com/s8mqRHSR it happens at varying frequency but about one in 25 will fail, and it isn't always the "last" value either, when testing it seems to get 'stuck' at a certain value no matter how many times I change it up.

我正在使用Flask_Sqlalchemy的动态绑定"示例

I am using the "dynamically bound" example of Flask_Sqlalchemy

db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    db.init_app(app)
    ... snip ...
    return app

在db.session中创建一个可访问的scoped_session.

Which creates a scoped_session accessible in db.session.

Apache的配置很长很复杂,但是其中包含

Apache config is long and complicated but includes the line

WSGIDaemonProcess pixel processes=5 threads=5 display-name='%{GROUP}'

如果需要,我可以发布更多信息.

I can post more information if required.

推荐答案

如果有人发现此线程存在相同的问题,请参考以解决问题.

For reference if anyone finds this thread with the same issue, I fixed my problem.

我的Flask App工厂功能在早期基于Flask教程而剩下的行app.app_context().push().不幸的是,从示例代码中删除了它,否则可能有人发现了它.在项目的重组过程中,这条线被排除在外,问题得以解决.不确定为什么或仅此行会导致此问题,并且仅针对部分但不是全部请求.

My Flask App factory function had the line app.app_context().push() leftover from the early days when it was based off a Flask tutorial. Unfortunately snipped out of the example code otherwise it might have been spotted by someone. During a restructuring of the project this line was left out and the problem fixed itself. Not sure why or how this line would cause this issue, and only for some but not all requests.

这篇关于Flask_Sqlalchemy与多线程Apache.会话与数据库不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆