Celery基本介绍
Celery架构及介绍
项目开发中经常碰到需要使用异步任务的场景,比如一个WEB请求中有运行时间很长的业务运算,如果不采用异步任务,会阻塞当前的web请求,影响用户体验
Celery的架构由三部分组成,消息中间件(message broker),任务执行单元(worker) 和 任务执行结果存储(task result store) 组成
消息中间件
Celery本身不提供消息服务,但是可以方便的和第三方提供的消息中间件集成。包括,RabbitMQ, Redis等等
任务执行单元
Worker是Celery提供的任务执行的单元,worker并发的运行在分布式的系统节点中。
任务结果存储
Task result store用来存储Worker执行的任务的结果,Celery支持以不同方式存储任务的结果,包括AMQP, redis等
Celery的优点
1、简单:一单熟悉了celery的工作流程后,配置和使用还是比较简单的
2、高可用:当任务执行失败或执行过程中发生连接中断,celery 会自动尝试重新执行任务
3、快速:一个单进程的celery每分钟可处理上百万个任务
4、灵活: 几乎celery的各个组件都可以被扩展及自定制
版本支持情况
Celery version 4.0 runs on
Python ❨2.7, 3.4, 3.5❩
PyPy ❨5.4, 5.5❩
This is the last version to support Python 2.7, and from the next version (Celery 5.x) Python 3.5 or newer is required.
If you’re running an older version of Python, you need to be running an older version of Celery:
Python 2.6: Celery series 3.1 or earlier.
Python 2.5: Celery series 3.0 or earlier.
Python 2.4 was Celery series 2.2 or earlier.
Celery is a project with minimal funding, so we don’t support Microsoft Windows. Please don’t open any issues related to that platform.
使用场景
-
异步任务:将耗时操作任务提交给Celery去异步执行,比如发送短信/邮件、消息推送、音视频处理等等
-
定时任务:定时执行某件事情,比如每天数据统计