Celery 分布式任务队列快速入门 以及在Django中动态添加定时任务

转自 金角大王 http://www.cnblogs.com/alex3714/articles/6351797.html

本节内容

Celery介绍和基本使用

在项目中如何使用celery

启用多个workers

Celery 定时任务

与django结合

通过django配置celery periodic task

一、Celery介绍和基本使用

Celery 是一个基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, 举几个实例场景中可用的例子:

  1. 你想对100台机器执行一条批量命令,可能会花很长时间,但你不想让你的程序等着结果返回,而是给你返回一个任务ID,你过一段时间只需要拿着这个任务id就可以拿到任务执行结果, 在任务执行ing进行时,你可以继续做其它的事情。
  2. 你想做一个定时任务,比如每天检测一下你们所有客户的资料,如果发现今天是客户的生日,就给他发个短信祝福

Celery 在执行任务时需要通过一个消息中间件来接收和发送任务消息,以及存储任务结果, 一般使用rabbitMQ or Redis,后面会讲

1.1 Celery有以下优点:

  1. 简单:一单熟悉了celery的工作流程后,配置和使用还是比较简单的
  2. 高可用:当任务执行失败或执行过程中发生连接中断,celery 会自动尝试重新执行任务
  3. 快速:一个单进程的celery每分钟可处理上百万个任务
  4. 灵活: 几乎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中动态添加定时任务的更多相关文章

  1. Celery 分布式任务队列快速入门

    Celery 分布式任务队列快速入门 本节内容 Celery介绍和基本使用 在项目中如何使用celery 启用多个workers Celery 定时任务 与django结合 通过django配置cel ...

  2. 【转】Celery 分布式任务队列快速入门

    Celery 分布式任务队列快速入门 本节内容 Celery介绍和基本使用 在项目中如何使用celery 启用多个workers Celery 分布式 Celery 定时任务 与django结合 通过 ...

  3. Celery分布式任务队列快速入门

    本节内容 1. Celery介绍和基本使用 2. 项目中使用Celery 3. Celery定时任务 4. Celery与Django结合 5. Django中使用计划任务 一  Celery介绍和基 ...

  4. day21 git & github + Celery 分布式任务队列

    参考博客: git & github 快速入门http://www.cnblogs.com/alex3714/articles/5930846.html git@github.com:liyo ...

  5. Celery动态添加定时任务

    背景 业务需求:用户可创建多个多人任务,需要在任务截止时间前一天提醒所有参与者 技术选型: Celery:分布式任务队列.实现异步与定时 django-celery-beat:实现动态添加定时任务,即 ...

  6. Django中简单添加HTML、css、js等文件(非正规添加,适合小白)

    Django中简单添加HTML.css.js等文件 首先申明下自己的环境, python版本3.65(亲测3.7版本有毒,没解决掉!) Django版本1.11.15(版本比较成熟,也可以用最新的版本 ...

  7. Celery 分布式任务队列入门

    一.Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery ...

  8. celery --分布式任务队列

    一.介绍 celery是一个基于python开发的分布式异步消息任务队列,用于处理大量消息,同时为操作提供维护此类系统所需的工具. 它是一个任务队列,专注于实时处理,同时还支持任务调度.如果你的业务场 ...

  9. Celery ---- 分布式队列神器 ---- 入门

    原文:http://python.jobbole.com/87238/ 参考:https://zhuanlan.zhihu.com/p/22304455 Celery 是什么? Celery 是一个由 ...

随机推荐

  1. GitLab服务器IP地址修改

    gitlab安装介绍:https://about.gitlab.com/downloads/#centos7 刚搭建好的gitlab在GitLab上新建一个项目test_gitlab,刚开始仓库地址是 ...

  2. InteliJ中文乱码;IDE快捷键使用

    启动服务器的时候出现如图 解决方法: 对服务器的位置进行编辑 增加如图的信息 -Dfile.encoding=UTF-8

  3. Redis实现高并发下的抢购、秒杀功能

    博主最近在项目中遇到了抢购问题!现在分享下.抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖" ...

  4. JavaScript中两种类型的全局对象/函数【转】

    Snandy Stop, thinking is the essence of progress. JavaScript中两种类型的全局对象/函数 这里所说的JavaScript指浏览器环境中的包括宿 ...

  5. 如何解决Android开发中的【java.lang.unsatisfiedlinkerror findLibrary returned null.】 错误

    将脉可寻的功能加入到自己的APP中时,需要在libs文件中添加.so文件和jar包 但是,加入.so文件后,仍然报错 在一番折腾之后,终于解决了,然而解决的方法很奇异- -. 在libs下新建一个ar ...

  6. ioctlsocket()函数是干什么用的?它返回值是什么?共有几个参数?它的各个参数是干什么用的?

    1. ioctlsocket()  简述:   控制套接口的模式. #include <winsock.h> int PASCAL FAR ioctlsocket( SOCKET s, l ...

  7. KindEditor上传图片

    <script type="text/javascript"> KindEditor.ready(function(K) { var editor1 = K.creat ...

  8. html中怎么样让div并排显示

    html中的div默认是流式显示,每个div会占用一整行 <html> <head> <meta http-equiv="Content-Type" ...

  9. ubuntu 编译安装 mod_wsgi

    在编译过程中遇到一些问题,记录下来方便别人使用. step1: 下载.windows下面会有编译好的包,Ubuntu没有需要自己编译. 地址: https://github.com/GrahamDum ...

  10. C#读取Excel的数据,并且以混合模式读取,防止数据类型变更

    /// <summary> /// Read Excel to DataSet /// </summary> /// <param name="filename ...