celery 快速入门教程 celery 定时器
当然首先得安装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 定时器的更多相关文章
- 专为设计师而写的GitHub快速入门教程
专为设计师而写的GitHub快速入门教程 来源: 伯乐在线 作者:Kevin Li 原文出处: Kevin Li 在互联网行业工作的想必都多多少少听说过GitHub的大名,除了是最大的开源项目 ...
- EntityFramework6 快速入门教程
EntityFramework6 快速入门教程 不得不说EF在国内实在是太小众,相关的技术文章真实屈指可数,而且很多文章都很旧了,里面使用的版本跟如今的EF6差别还是比较大.我刚开始弄这个的时候真是绕 ...
- Apple Watch开发快速入门教程
Apple Watch开发快速入门教程 试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ...
- 指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程
指示灯组与3个复位按钮的介绍Arduino Yun快速入门教程 1.4.2 指示灯组 指示灯组的放大图如图1.5所示. 图1.5 指示灯组 各个指示灯对应的功能如下: q RX:对应于0号端口, ...
- 游戏控制杆OUYA游戏开发快速入门教程
游戏控制杆OUYA游戏开发快速入门教程 1.2.2 游戏控制杆 游戏控制杆各个角度的视图,如图1-4所示,它的硬件规格是本文选自OUYA游戏开发快速入门教程大学霸: 图1-4 游戏控制杆各个角度的 ...
- Query 快速入门教程
Query 快速入门教程 http://www.365mini.com/page/jquery-quickstart.htm#what_is_jquery jquery常用方法及使用示例汇总 http ...
- Realm for Android快速入门教程
介绍 如果你关注安卓开发的最新趋势,你可能已经听说过Realm.Realm是一个可以替代SQLite以及ORMlibraries的轻量级数据库. 相比SQLite,Realm更快并且具有很多现代数据库 ...
- CMake快速入门教程-实战
http://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ http://blog.csdn.net/dbzhang800/article/detai ...
- .NET Core 快速入门教程
.NET Core 快速学习.入门系列教程.这个入门系列教程主要跟大家聊聊.NET Core的前世今生,以及Windows.Linux(CentOS.Ubuntu)基础开发环境的搭建.第一个.NET ...
随机推荐
- Hadoop下面WordCount运行详解
单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为MapReduce版"Hello World",该程序的完整代码可以在Hadoop安装包的"src/ ...
- DirectWrite文字排版——字符串去尾
DirectWrite是 DirectX 家族中专门用来做文本处理的部分,主要配合Direct2D进行渲染工作. 一.字符串去尾介绍 在文字渲染中,不免会遇到字符串去尾的需求.字符串去尾指的是:当字符 ...
- 记第二次使用php开发项目之绝不重复自己
严格说起来,自己并非一个合格的php程序员.第一次使用php开发,不过是因为游戏上线,需要一个统计管理后台和GM后台,因为招聘已经来不及,所以我就上前线了! 凭着对php语法的一点点记忆(大学的时候学 ...
- 【Beta阶段】团队源代码管理
0. 快速上手与理解 如果你的团队来了一个新队员,有一台全新的机器,你们是否有一个文档,只要设置了相应的权限,她就可以根据文档,从头开始搭建环境,并成功地把最新.最稳定版本的软件编译出来,并运行必要的 ...
- [C#] 语法之Attribute
在c#中,定义类的成员,可以定义Property称为属性.Attribute就称为特性. 在FCL中,有内置的Attribute.如: Condition[Attribute]:在什么条件可以调用.( ...
- C#开发ActiveX网页截图控件
故事背景:Java组的小伙伴需要一个能在IE(还是6...)下截图并返回给网页的功能,但是IE做起来很麻烦(可能根本做不到),于是找到我写一个ActiveX控件实现此功能,想着可能还有其他小伙伴需要这 ...
- HTML--Table布局
<DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" Content= ...
- MVC显示详细记录Without Entity Framework
看过此篇<MVC用非Entity Framework将数据显示于视图(二)>http://www.cnblogs.com/insus/p/3364482.html 了解到把数据库中数据表的 ...
- 怎样在C#中从数据库中读取数据(数据读取器)
实现在C#中通过语句,查询数据库中的数据 SqlConnection con = null; //创建SqlConnection 的对象 try //try里面放可能出现错误的代码 ...
- sql apply
可以给表值函数传column,而join不可以