31
celery变量共享
source link: http://yongqing.is-programmer.com/posts/215408.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
妙音posted @ 2020年6月21日 09:19 inpython with tagscelery , 7 阅读
问题
很多情况下我们想让task共享变量,该怎么做?
celery的并发原理
celery的并发任务池,有eventlet, gevent, prefork, thread类型
eventlet/gevent协程: 只有一个进程一个线程, 全局变量在task之间共享
prefork属于multiprocessing: multiprocessing全局变量也是共享的
thread多线程: 全局变量共享
验证方法
用ab命令模拟大量并发,很容易测试出来
ab -n 1000 -c 100 -p ./post.txt -T application/json http://xxxx:5000/xxx
结论
1. celery如果访问数据库, gpu等资源, 不用担心多次加载
2. 注意: 如果在task中初始化全局变量, 初始化较慢, 同时又收到大量task请求,可能会导致初始化多次
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK