Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用Gunicorn的preload进行多进程全局变量共享,引发bug #3206

Closed
wylycool opened this issue May 10, 2024 · 2 comments
Closed

Comments

@wylycool
Copy link

wylycool commented May 10, 2024

语言:python3.11,flask2.0,Gunicorn==20.0.4
系统:linux、macos
现象:当用命令gunicorn --preload.... 启动flask后,workers = 2,并发调用50次测试接口,接口内部逻辑只有一句sql是 select id from table where id=123
理论结果:应该是50次全部都是一样的查询结果;
实际结果:其中总会有1-2次出现mysql查询的结果为空,是怎么回事?mysql是flask启动后在init文件中做的连接池对象的初始化,应该会被全局共享。当我worker改为1,问题消失,或者当我去除preload参数,保证每个子进程都有各自变量时,问题也会消失,所以看起来问题像是因为共享了全局mysql连接池对象而导致的问题,具体gunicorn内部是什么原因导致的,有朋友知道吗?

@benoitc
Copy link
Owner

benoitc commented May 21, 2024

ensure your sql lib is not shared across workers. This can explain your issue.

@benoitc benoitc closed this as completed May 21, 2024
@wylycool
Copy link
Author

ensure your sql lib is not shared across workers. This can explain your issue.

👌🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants