Celery 分布式任务队列快速入门 以及在Django中动态添加定时任务
Celery 分布式任务队列快速入门 以及在Django中动态添加定时任务
转自 金角大王 http://www.cnblogs.com/alex3714/articles/6351797.html
本节内容
Celery介绍和基本使用
在项目中如何使用celery
启用多个workers
Celery 定时任务
与django结合
通过django配置celery periodic task
一、Celery介绍和基本使用
Celery 是一个基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, 举几个实例场景中可用的例子:
- 你想对100台机器执行一条批量命令,可能会花很长时间,但你不想让你的程序等着结果返回,而是给你返回一个任务ID,你过一段时间只需要拿着这个任务id就可以拿到任务执行结果, 在任务执行ing进行时,你可以继续做其它的事情。
- 你想做一个定时任务,比如每天检测一下你们所有客户的资料,如果发现今天是客户的生日,就给他发个短信祝福
Celery 在执行任务时需要通过一个消息中间件来接收和发送任务消息,以及存储任务结果, 一般使用rabbitMQ or Redis,后面会讲
1.1 Celery有以下优点:
- 简单:一单熟悉了celery的工作流程后,配置和使用还是比较简单的
- 高可用:当任务执行失败或执行过程中发生连接中断,celery 会自动尝试重新执行任务
- 快速:一个单进程的celery每分钟可处理上百万个任务
- 灵活: 几乎celery的各个组件都可以被扩展及自定制
Celery基本工作流程图

1.2 Celery安装使用
Celery的默认broker是RabbitMQ, 仅需配置一行就可以
`broker_url ``=` `'amqp://guest:guest@localhost:5672//'`
rabbitMQ 没装的话请装一下,安装看这里 http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#id3
使用Redis做broker也可以
安装redis组件
`$ pip install ``-``U ``"celery[redis]"`
?配置
Configuration is easy, just configure the location of your Redis database:
app.conf.broker_url = 'redis://localhost:6379/0'
Where the URL is in the format of:
redis://:password@hostname:port/db_number
all fields after the scheme are optional, and will default to localhost on port 6379, using database 0.
如果想获取每个任务的执行结果,还需要配置一下把任务结果存在哪
If you also want to store the state and return values of tasks in Redis, you should configure these settings:
app.conf.result_backend = 'redis://localhost:6379/0'
1. 3 开始使用Celery啦
安装celery模块
`$ pip install celery`
创建一个celery application 用来定义你的任务列表
创建一个任务文件就叫tasks.py吧
`from` `celery ``import` `Celery` `app ``=` `Celery(``'tasks'``,`` ``broker``=``'redis://localhost'``,`` ``backend``=``'redis://localhost'``)` `@app``.task``def` `add(x,y):`` ``print``(``"running..."``,x,y)`` ``return` `x``+``y`
启动Celery Worker来开始监听并执行任务
`$ celery -A tasks worker --loglevel=info`
调用任务
再打开一个终端, 进行命令行模式,调用任务
`>>> ``from` `tasks ``import` `add``>>> add.delay(``4``, ``4``)`
看你的worker终端会显示收到一个任务,此时你想看任务结果的话,需要在调用任务时赋值个变量
`>>> result ``=` `add.delay(``4``, ``4``)`
The ready() method returns whether the task has finished processing or not:
>>> result.ready ()
False
You can wait for the result to complete, but this is rarely used since it turns the asynchronous call into a synchronous one:
>>> result.get( timeout = 1 )
8
In case the task raised an exception, get() will re-raise the exception, but you can override this by specifying the propagate argument:
>>> result.get(propagate=False )
If the task raised an exception you can also gain access to the original traceback:
>>> result.traceback
…
二、在项目中如何使用celery
可以把celery配置成一个应用
目录格式如下
`proj``/__init__``.py`` ``/celery``.py`` ``/tasks``.py`
proj/celery.py内容
`from` `__future__ ``import` `absolute_import, unicode_literals``from` `celery ``import` `Celery` `app ``=` `Celery(``'proj'``,`` ``broker``=``'amqp://'``,`` ``backend``=``'amqp://'``,`` ``include``=``[``'proj.tasks'``])` `# Optional configuration, see the application user guide.``app.conf.update(`` ``result_expires``=``3600``,``)` `if` `__name__ ``=``=` `'__main__'``:`` ``app.start()`
proj/tasks.py中的内容
[
](javascript:void(0)
Celery 分布式任务队列快速入门 以及在Django中动态添加定时任务的更多相关文章
- Celery 分布式任务队列快速入门
Celery 分布式任务队列快速入门 本节内容 Celery介绍和基本使用 在项目中如何使用celery 启用多个workers Celery 定时任务 与django结合 通过django配置cel ...
- 【转】Celery 分布式任务队列快速入门
Celery 分布式任务队列快速入门 本节内容 Celery介绍和基本使用 在项目中如何使用celery 启用多个workers Celery 分布式 Celery 定时任务 与django结合 通过 ...
- Celery分布式任务队列快速入门
本节内容 1. Celery介绍和基本使用 2. 项目中使用Celery 3. Celery定时任务 4. Celery与Django结合 5. Django中使用计划任务 一 Celery介绍和基 ...
- day21 git & github + Celery 分布式任务队列
参考博客: git & github 快速入门http://www.cnblogs.com/alex3714/articles/5930846.html git@github.com:liyo ...
- Celery动态添加定时任务
背景 业务需求:用户可创建多个多人任务,需要在任务截止时间前一天提醒所有参与者 技术选型: Celery:分布式任务队列.实现异步与定时 django-celery-beat:实现动态添加定时任务,即 ...
- Django中简单添加HTML、css、js等文件(非正规添加,适合小白)
Django中简单添加HTML.css.js等文件 首先申明下自己的环境, python版本3.65(亲测3.7版本有毒,没解决掉!) Django版本1.11.15(版本比较成熟,也可以用最新的版本 ...
- Celery 分布式任务队列入门
一.Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery ...
- celery --分布式任务队列
一.介绍 celery是一个基于python开发的分布式异步消息任务队列,用于处理大量消息,同时为操作提供维护此类系统所需的工具. 它是一个任务队列,专注于实时处理,同时还支持任务调度.如果你的业务场 ...
- Celery ---- 分布式队列神器 ---- 入门
原文:http://python.jobbole.com/87238/ 参考:https://zhuanlan.zhihu.com/p/22304455 Celery 是什么? Celery 是一个由 ...
随机推荐
- 用Docker搭建WordPress
环境准备 CentOS 安装docker https://yeasy.gitbooks.io/docker_practice/content/install/centos.html docker-co ...
- Exception (3) Java exception handling best practices
List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...
- C# 加密(Encrypt) 解密(Decrypt) 操作类 java与 C# 可以相互加密解密
public sealed class EncryptUtils { #region Base64加密解密 /// <summary> /// Base64加密 /// </summ ...
- 设计模式13---桥接模式(Bridge Pattern)
桥接模式将抽象与具体实现分离,使得抽象与具体实现可以各自改变互不影响.桥接模式属于设计模式中的结构模式. 桥梁模式涉及的角色 抽象(Abstraction)角色:抽象定义,引用对接口对象的引用. 重新 ...
- SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...
- JavaScript - this详解 (二)
用栗子说this Bug年年有,今年特别多 对于JavaScript这么灵活的语言来说,少了this怎么活! function 函数 this 对于没有实例化的function,我们称之为函数,即没有 ...
- js链式调用
我们都很熟悉jQuery了,只能jQuery中一种非常牛逼的写法叫链式操作 * $('#div').css('background','#ccc').removeClass('box').stop() ...
- Java网络编程のOSI
我们可以把客户机和远程服务器理解为主机A和主机B,用户和主机A可以通过主机A中的应用程序进行交互,主机A与主机B之间交互则是通过计算机网络通信进行的. 网络中每台机器称为节点.大多数节点是计算机,此外 ...
- ASP.NET mvc4 Controllder 同步还是异步
从抽象类Controller 的定义可以看出他 同时实现了 IAsyncController, IController public abstract class Controller : Contr ...
- vmware开发包之vSphere Management SDK
vSphere Management SDK关于vmware管理等相关功能的.net和java的代码示例. 在其5.5的解压包中 eam:esx agent management表示esx虚机生产环境 ...