uwsgi + flask中的数据库一致性

时间:2015-11-04 13:35:22

标签: python mysql flask uwsgi

使用uwsgi + flask时,我遇到了有关数据库一致性的问题。谢谢你的帮助!

以下是数据库中的表格:

id token school degree department enrollment

首先,我更新令牌等于e2477cdffa2ab00b178dc17a805a75a0的学校。 然后,我使用上面相同的标记选择学校。 我正在使用SQLAlchemy访问flask中的数据库。

如果uwsgi.ini中的线程值配置为2: 更新后,第一个(第三个,第五个...)选择返回旧学校值,但第二个(第四个,第六个,第八个...)选择返回新值。

如果uwsgi.ini中的线程值配置为1,则在更新后,所有选择都会返回新值。

这是我的uwsgi.ini和相关代码:

[uwsgi]
socket = 127.0.0.1:8001
chdir = /home/www/app
wsgi-file = main.py
callable = app
processes = 1
threads = 3
stats = 127.0.0.1:9191
buffer-size = 32768
uid = root
gid = root
logto = /home/www/logs/uwsgi.log
pidfile = /tmp/uwsgi.pid

def editschooldb(token,school,degree,department,enrollment):
    u = User.query.filter_by(token=token).first()

    if u!= None:
        u.school = school
        u.degree = degree
        u.department = department
        u.enrollment = enrollment
        try:
            db.session.add(u)
            db.session.commit()
            return 0
        except Exception, e:
            db.session.rollback()
            return 1
    else:
        return 2

以下是tcpdump(threads = 2)的结果,从中我们可以看到在09:43:06烧瓶进程点击mysqlserver并选择返回“school”:“”,但是在09:43:28有另一个选择,返回“学校”:“test123”。

root@iZ25ko0w5hcZ:/home/www# tcpdump -i lo port 3306
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
09:43:06.187465 IP this.is.my.ip.45686 > this.is.my.ip.mysql: Flags [P.], seq 2937500406:2937501074, ack 1677732916, win 3637, options [nop,nop,TS val 119353277 ecr 119313822], length 668
09:43:06.187809 IP this.is.my.ip.mysql > this.is.my.ip.45686: Flags [P.], seq 1:1308, ack 668, win 425, options [nop,nop,TS val 119353277 ecr 119353277], length 1307
09:43:06.187823 IP this.is.my.ip.45686 > this.is.my.ip.mysql: Flags [.], ack 1308, win 3637, options [nop,nop,TS val 119353277 ecr 119353277], length 0
09:43:28.429654 IP this.is.my.ip.45689 > this.is.my.ip.mysql: Flags [P.], seq 1240221793:1240222461, ack 75923549, win 3637, options [nop,nop,TS val 119358838 ecr 119350800], length 668
09:43:28.429747 IP this.is.my.ip.mysql > this.is.my.ip.45689: Flags [P.], seq 1:1315, ack 668, win 436, options [nop,nop,TS val 119358838 ecr 119358838], length 1314
09:43:28.429759 IP this.is.my.ip.45689 > this.is.my.ip.mysql: Flags [.], ack 1315, win 3637, options [nop,nop,TS val 119358838 ecr 119358838], length 0

root@iZ25ko0w5hcZ:~# curl -i -H "Content-type: application/json" -X POST -d '{"token":"e2477cdffa2ab00b178dc17a805a75a0"}' "http://this.is.my.ip:8080/getprofile"
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Thu, 05 Nov 2015 01:43:06 GMT
Content-Type: application/json
Content-Length: 317
Connection: keep-alive

{
  "birthday": "", 
  "degree": "", 
  "department": "", 
  "enrollment": "", 
  "gender": "\u5973", 
  "hobby": "", 
  "id": 2, 
  "name": "", 
  "phone": "", 
  "preference": "", 
  "reason": "", 
  "school": "", 
  "state": "successful", 
  "token": "e2477cdffa2ab00b178dc17a805a75a0", 
  "username": "just2012"
}
root@iZ25ko0w5hcZ:~# curl -i -H "Content-type: application/json" -X POST -d '{"token":"e2477cdffa2ab00b178dc17a805a75a0"}' "http://this.is.my.ip:8080/getprofile"
HTTP/1.1 200 OK
Server: nginx/1.2.6
Date: Thu, 05 Nov 2015 01:43:28 GMT
Content-Type: application/json
Content-Length: 324
Connection: keep-alive

{
  "birthday": "", 
  "degree": "", 
  "department": "", 
  "enrollment": "", 
  "gender": "\u5973", 
  "hobby": "", 
  "id": 2, 
  "name": "", 
  "phone": "", 
  "preference": "", 
  "reason": "", 
  "school": "test123", 
  "state": "successful", 
  "token": "e2477cdffa2ab00b178dc17a805a75a0", 
  "username": "just2012"
}

0 个答案:

没有答案