Flask-Sqlalchemy BEGIN丢失似乎导致会话不同步

时间:2018-07-24 18:46:14

标签: python mysql apache flask sqlalchemy

更新到我的最后一个问题,请阅读this post了解背景信息。和This reddit thread是我尝试过的。我已进一步调查。

TL; DR是,对于某些请求,我似乎得到了旧数据。我通过发送POST来更新对象和几个GET打印该对象的属性来进行测试。通常,响应是好的,但是每隔一段时间我就会得到旧数据。创建与其他对象有关系的对象时,这是一个大问题,如果由于某种原因不存在外键对象,则创建将失败。

示例:

format@pixel:~$ curl -X POST https://example.com/app/region/rename/5/Old
Success
format@pixel:~$ curl -X POST https://example.com/app/region/rename/5/New
Success
format@pixel:~$ for i in {1..25}; do  echo ; curl -X GET https://example.com/app/region/name/5/; done
New
New
New
New
New
New
Old
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New

我打开了SQLALCHEMY_ECHO和SQLALCHEMY_COMMIT_ON_TEARDOWN

app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

并更改了代码以打印更多信息。

当请求正确时,通常使用第一个日志片段。您可以在拆卸时看到隐式的BEGIN和COMMIT。

[Tue Jul 24 17:49:23.217666 2018] [wsgi:error] [pid 27919:tid 139730801542912] --- Get Start ---
[Tue Jul 24 17:49:23.219964 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,219 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
[Tue Jul 24 17:49:23.220053 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:BEGIN (implicit)
[Tue Jul 24 17:49:23.221282 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,221 INFO sqlalchemy.engine.base.Engine SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.221321 2018] [wsgi:error] [pid 27919:tid 139730801542912] FROM region 
[Tue Jul 24 17:49:23.221336 2018] [wsgi:error] [pid 27919:tid 139730801542912] WHERE region.id = %s
[Tue Jul 24 17:49:23.221356 2018] [wsgi:error] [pid 27919:tid 139730801542912] 
[Tue Jul 24 17:49:23.221414 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.221433 2018] [wsgi:error] [pid 27919:tid 139730801542912] FROM region 
[Tue Jul 24 17:49:23.221447 2018] [wsgi:error] [pid 27919:tid 139730801542912] WHERE region.id = %s
[Tue Jul 24 17:49:23.221465 2018] [wsgi:error] [pid 27919:tid 139730801542912] 
[Tue Jul 24 17:49:23.221683 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,221 INFO sqlalchemy.engine.base.Engine (5,)
[Tue Jul 24 17:49:23.221767 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:(5,)
[Tue Jul 24 17:49:23.222922 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23.222868 New
[Tue Jul 24 17:49:23.222990 2018] [wsgi:error] [pid 27919:tid 139730801542912] --- Get End ---
[Tue Jul 24 17:49:23.224867 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,224 INFO sqlalchemy.engine.base.Engine COMMIT
[Tue Jul 24 17:49:23.224946 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:COMMIT

下一个代码段通常用于请求不正确的情况。您可以看到它与以前一样执行SELECT,但是没有BEGIN或COMMIT。不知道这意味着什么或为什么会发生,但是使我相信问题出在SQLAlchemy或MySQL层。

[Tue Jul 24 17:49:23.288116 2018] [wsgi:error] [pid 27919:tid 139730654033664] --- Get Start ---
[Tue Jul 24 17:49:23.290049 2018] [wsgi:error] [pid 27919:tid 139730654033664] 2018-07-24 17:49:23,289 INFO sqlalchemy.engine.base.Engine SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.290079 2018] [wsgi:error] [pid 27919:tid 139730654033664] FROM region 
[Tue Jul 24 17:49:23.290092 2018] [wsgi:error] [pid 27919:tid 139730654033664] WHERE region.id = %s
[Tue Jul 24 17:49:23.290109 2018] [wsgi:error] [pid 27919:tid 139730654033664] 
[Tue Jul 24 17:49:23.290168 2018] [wsgi:error] [pid 27919:tid 139730654033664] INFO:sqlalchemy.engine.base.Engine:SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.290186 2018] [wsgi:error] [pid 27919:tid 139730654033664] FROM region 
[Tue Jul 24 17:49:23.290197 2018] [wsgi:error] [pid 27919:tid 139730654033664] WHERE region.id = %s
[Tue Jul 24 17:49:23.290214 2018] [wsgi:error] [pid 27919:tid 139730654033664] 
[Tue Jul 24 17:49:23.290422 2018] [wsgi:error] [pid 27919:tid 139730654033664] 2018-07-24 17:49:23,290 INFO sqlalchemy.engine.base.Engine (5,)
[Tue Jul 24 17:49:23.290499 2018] [wsgi:error] [pid 27919:tid 139730654033664] INFO:sqlalchemy.engine.base.Engine:(5,)
[Tue Jul 24 17:49:23.291567 2018] [wsgi:error] [pid 27919:tid 139730654033664] 2018-07-24 17:49:23.291529 @@OLD@@
[Tue Jul 24 17:49:23.291591 2018] [wsgi:error] [pid 27919:tid 139730654033664] --- Get End ---

从时间戳中可以看到,好的请求和不好的请求在字面上是正确的,在这个不好的请求之后也有几个好的请求,似乎没有韵律或理由。

我正在使用mysqlclient 1.3.13,SQLAlchemy 1.2.10,Flask-SQLAlchemy 2.3.2,并在今天早些时候运行了apt-get upgrade

1 个答案:

答案 0 :(得分:0)

作为参考,如果有人发现具有相同问题的该线程,我已解决了问题。

我的Flask App工厂功能最初是基于Flask教程而剩下的行@CurrentLastFolio + 1。在项目的重组过程中,这条线被排除在外,问题得以解决。不确定为什么或仅此行会导致此问题,仅针对部分但不是全部请求。