当然首先得安装celery和rabbitmq-server,如果有redis需要安装redis

安装Redis
$ yum install redis
启动 Redis $redis-server
检查Redis是否在工作? $redis-cli
这将打开一个Redis提示,如下图所示: redis 127.0.0.1:6379>
上面的提示127.0.0.1是本机的IP地址,6379为Redis服务器运行的端口。现在输入PING命令,如下图所示。 redis 127.0.0.1:6379> ping
PONG
这说明你已经成功地安装Redis在您的机器上。

  

tasks.py

from celery import Celery
from time import sleep app = Celery('tasks', backend='amqp', broker='amqp://guest@localhost//')
#app = Celery('tasks', backend='redis://localhost', broker='amqp://guest@localhost//')
#app = Celery('tasks', backend='redis://localhost:6379/1', broker='amqp://guest@localhost//')
#app = Celery('tasks', backend='redis://localhost:6379/1', broker='redis://localhost:6379/0')
#app = Celery('taskwb', backend='amqp://guest@localhost:5672/0', broker='amqp://guest@localhost:5672/1') @app.task
def add(x, y):
sleep(5)
return x + y

保存后执行celery -A tasks worker --loglevel=info,可以看到提示页面

[wenbin celery]$ celery -A tasks worker --loglevel=info
[-- ::,: WARNING/MainProcess] /usr/lib/python2./site-packages/celery/apps/worker.py:: CDeprecationWarning:
Starting from version 3.2 Celery will refuse to accept pickle by default. The pickle serializer is a security concern as it may give attackers
the ability to execute any command. It's important to secure
your broker from unauthorized access when using pickle, so we think
that enabling pickle should require a deliberate action and not be
the default choice. If you depend on pickle then you should set a setting to disable this
warning and to be sure that everything will continue working
when you upgrade to Celery 3.2:: CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml'] You must only enable the serializers that you will actually use. warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) -------------- celery@wb-test-multiple-portal v3.1.13 (Cipater)
---- **** -----
--- * *** * -- Linux-2.6.-.el6.x86_64-x86_64-with-centos-6.6-Final
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x1071e50
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: amqp
- *** --- * --- .> concurrency: (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery

然后新开一个窗口进入python命令行:

>>>from tasks import add

>>>res = add.delay(4, 3)

可以看到之前启动celery的那边有信息输出,并且过5s后执行了结果(因为我们add中sleep了5秒)

[2016-03-07 15:21:43,153: INFO/MainProcess] Received task: tasks.add[fd523296-8b99-4530-a77e-3fa56cc19a5d]
[2016-03-07 15:21:48,184: INFO/MainProcess] Task tasks.add[fd523296-8b99-4530-a77e-3fa56cc19a5d] succeeded in 5.02967603202s: 7

说明我们的demo已经成功了。

有一个小错误自己发现的,我之后又开了一个celery运行的窗口(其实是忘记关了之前的),发现在python命令行执行delay的时候只有一半成功了。。。这是个神奇的东西,能够自动检测到另一个的存在,然后分配任务;然后我又再运行一个celery,这样发现原来3个celery都能轮流来进行任务的执行,简直是个神奇的东西。。。

定时器功能

使用定时器功能首先得配置一下schedule,刚刚我们是直接在Celery函数加入配置,现在我们专门用一个文件来放配置文件,schedule也会写在这里面。

修改tasks.py

from celery import Celery
from time import sleep
import celeryconfig app = Celery('tasks')#, backend='amqp', broker='amqp://guest@localhost//')
app.config_from_object('celeryconfig') @app.task
def add(x, y):
sleep()
return x + y

增加配置文件celeryconfig.py

from celery.schedules import crontab

BROKER_URL = 'amqp://guest@localhost//'
CELERY_RESULT_BACKEND = 'amqp://'
CELERYBEAT_SCHEDULE={
"every-1-minute": {
'task': 'tasks.add',
'schedule': crontab(minute='*/1'),
'args': (,)
}
}

表示一分钟触发一次add的函数,args是传入的参数,表示一分钟执行一次add(5,6),注意如果再添加一个任务,不能与every-1-minute重复,不然只有最后一个生效了。

然后执行celery -A tasks worker -B --loglevel=info    就能够增加触发beat任务了,会在本地生成一个celerybeat-schedule文件。最好在-B后面加一个 -s /tmp/celerybeat-schedule  ,不然很可能导致当前目录没有写权限而报permission refused

[wenbin@wb-test-multiple-portal celery]$ celery -A tasks worker -B --loglevel=info
[-- ::,: WARNING/MainProcess] /usr/lib/python2./site-packages/celery/apps/worker.py:: CDeprecationWarning:
Starting from version 3.2 Celery will refuse to accept pickle by default. The pickle serializer is a security concern as it may give attackers
the ability to execute any command. It's important to secure
your broker from unauthorized access when using pickle, so we think
that enabling pickle should require a deliberate action and not be
the default choice. If you depend on pickle then you should set a setting to disable this
warning and to be sure that everything will continue working
when you upgrade to Celery 3.2:: CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml'] You must only enable the serializers that you will actually use. warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) -------------- celery@wb-test-multiple-portal v3.1.13 (Cipater)
---- **** -----
--- * *** * -- Linux-2.6.-.el6.x86_64-x86_64-with-centos-6.6-Final
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x2a095d0
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: amqp://
- *** --- * --- .> concurrency: (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery [tasks]
. tasks.add [-- ::,: INFO/Beat] beat: Starting...
[-- ::,: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[-- ::,: INFO/MainProcess] mingle: searching for neighbors
[-- ::,: INFO/MainProcess] mingle: all alone
[-- ::,: WARNING/MainProcess] celery@wb-test-multiple-portal ready.
[-- ::,: INFO/Beat] Scheduler: Sending due task every--minute (tasks.add)
[-- ::,: INFO/MainProcess] Received task: tasks.add[a81182bd--4d4a-b3cd-a81f7900a12b]
[-- ::,: INFO/MainProcess] Task tasks.add[a81182bd--4d4a-b3cd-a81f7900a12b] succeeded in .02911209001s:

当然也可以分开执行celery和beat,当然需要两个窗口了

celery -A tasks worker --loglevel=info

celery -A tasks beat

参考文献:

http://docs.jinkan.org/docs/celery/index.html

http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

http://my.oschina.net/hochikong/blog/419191?p={{currentPage-1}}

http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html

celery 快速入门教程 celery 定时器的更多相关文章

  1. 专为设计师而写的GitHub快速入门教程

    专为设计师而写的GitHub快速入门教程 来源: 伯乐在线 作者:Kevin Li     原文出处: Kevin Li 在互联网行业工作的想必都多多少少听说过GitHub的大名,除了是最大的开源项目 ...

  2. EntityFramework6 快速入门教程

    EntityFramework6 快速入门教程 不得不说EF在国内实在是太小众,相关的技术文章真实屈指可数,而且很多文章都很旧了,里面使用的版本跟如今的EF6差别还是比较大.我刚开始弄这个的时候真是绕 ...

  3. Apple Watch开发快速入门教程

     Apple Watch开发快速入门教程  试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ...

  4. 指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程

    指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程 1.4.2  指示灯组 指示灯组的放大图如图1.5所示. 图1.5  指示灯组 各个指示灯对应的功能如下: q  RX:对应于0号端口, ...

  5. 游戏控制杆OUYA游戏开发快速入门教程

    游戏控制杆OUYA游戏开发快速入门教程 1.2.2  游戏控制杆 游戏控制杆各个角度的视图,如图1-4所示,它的硬件规格是本文选自OUYA游戏开发快速入门教程大学霸: 图1-4  游戏控制杆各个角度的 ...

  6. Query 快速入门教程

    Query 快速入门教程 http://www.365mini.com/page/jquery-quickstart.htm#what_is_jquery jquery常用方法及使用示例汇总 http ...

  7. Realm for Android快速入门教程

    介绍 如果你关注安卓开发的最新趋势,你可能已经听说过Realm.Realm是一个可以替代SQLite以及ORMlibraries的轻量级数据库. 相比SQLite,Realm更快并且具有很多现代数据库 ...

  8. CMake快速入门教程-实战

    http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ http://blog.csdn.net/dbzhang800/article/detai ...

  9. .NET Core 快速入门教程

    .NET Core 快速学习.入门系列教程.这个入门系列教程主要跟大家聊聊.NET Core的前世今生,以及Windows.Linux(CentOS.Ubuntu)基础开发环境的搭建.第一个.NET ...

随机推荐

  1. Python内置函数解析

    我们知道,为了方便使用,python内置了一系列常用及关键的函数,如type().下面将对这些函数进行逐一分析.解释. Python内置函数表: 1. abs():返回绝对值.如abs(-1)= 1. ...

  2. SQL Server 诊断查询-(1)

    Query #1 is Version Info. SQL and OS Version information for current instance SELECT @@SERVERNAME AS ...

  3. Gradle学习系列之二——创建Task的多种方法

    在本系列的上篇文章中,我们讲到了Gradle入门,在本篇文章中我们将讲到创建Task的多种方法. 请通过以下方式下载本系列文章的Github示例代码: git clone https://github ...

  4. ListBox实现拖拽排序功能

    1.拖拽需要实现的事件包括: PreviewMouseLeftButtonDown LBoxSort_OnDrop 具体实现如下: private void LBoxSort_OnPreviewMou ...

  5. ADO.NET 连接方式和非链接方式访问数据库

    一.//连接方式访问数据库的主要步骤(利用DataReader对象实现数据库连接模式) 1.创建连接对象(连接字符串) SqlConnection con = new SqlConnection(Co ...

  6. 已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。

    错误:已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性. 或者 错误:反序列化操作“GetAllUserData ...

  7. 【转】MSMQ 微软消息队列 简单 示例

    MSMQ它的实现原理是:消息的发送者把自己想要发送的信息放入一个容器中(我们称之为Message),然后把它保存至一个系统公用空间的消息队列(Message Queue)中:本地或者是异地的消息接收程 ...

  8. (转载)IO-同步、异步、阻塞、非阻塞

    一.概述 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不 ...

  9. IIS7.5支持解析读取.json文件数据

    在站点中添加 MIME类型去支持Json文件的解析 添加mime类型 文件扩展名:.json MIME类型:application/json 添加成功后即可. 如果不能直接操作iis也可以直接在web ...

  10. C#的pictureBox怎样使用多张图片简单切换

    首先,先创建一个新的winform项目ImageTest,选择窗体,起名我ImageForm,在ImageForm拉一个picturebox控件,一个控制器trimer,一个相册imageList,在 ...