uWSGI+Flask 并发 MySQL 查询结果会串的解决办法

最近自己的 Python Web 程序出了一个非常诡异的问题:不同用户的数据库查询结果会串。A 用户会读到 B 用户的数据库查询结果,B 用户可能又读到了 C 用户的查询结果,乱成一团,Sentry 疯狂报警。一开始以为是自己程序改错了,直到后来慢慢怀疑是修改了 uWSGI 配置文件引起的问题。

在发生故障之前,我的 uWSGI 并发模式是多线程,因为 Python 的 GIL 有限制,所以改成了多进程。事实上,就是这个引起了问题。

根据官方的 Things to know 页面(https://uwsgi-docs.readthedocs.io/en/latest/ThingsToKnow.html),uWSGI 的多进程对于 fork 机制是有一些使用不当的。默认情况下会在 app 初始化之后 fork,而不是在初始化之前,因为我的程序在初始化的时候建立连接池,所以导致了数据库连接池混乱:

uWSGI tries to (ab)use the Copy On Write semantics of the fork() call whenever possible. By default it will fork after having loaded your applications to share as much of their memory as possible. If this behavior is undesirable for some reason, use the lazy-apps option. This will instruct uWSGI to load the applications after each worker’s fork(). Beware as there is an older options named lazy that is way more invasive and highly discouraged (it is still here only for backward compatibility)

所以,如果要使用多进程的话,我们需要把 lazy-apps 这个选项修改为 true。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值