A simple case to use Celery:
Prerequisites:
1: Install RabbitMQ as it would be used as message broker for Celery. In windows, it would create a service, make sure the service is started.
2: Install Celery: pip install celery
Meat and Potatoes:
Senario 1: don't specify the backend for celery, if we don't care about the result
1. Create a module named tasks.py
from __future__ import absolute_import
from celery import Celery
import time app = Celery('tasks', broker='amqp://guest@localhost:5672//') @app.task
def add(x, y):
print 'hello celery'
time.sleep(10)
return x + y
2. Start Celery worker
celery worker -A tasks --loglevel=INFO
You would see the console output like below,
-------------- celery@YUFA-7W v3.1.10 (Cipater)
---- **** -----
--- * *** * -- Windows-7-6.1.7601-SP1
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasks:0x36871d0
- ** ---------- .> transport: amqp://guest@localhost:5672//
- ** ---------- .> results: disabled
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery exchange=celery(direct) key=celery [tasks]
. tasks.add [2014-03-26 15:43:11,263: INFO/MainProcess] Connected to amqp://guest@127.0.0.1:5672//
[2014-03-26 15:43:11,285: INFO/MainProcess] mingle: searching for neighbors
[2014-03-26 15:43:12,293: INFO/MainProcess] mingle: all alone
[2014-03-26 15:43:12,302: WARNING/MainProcess] celery@YUFA-7W ready.
3. Test the method
Call the function "add",
>>> from tasks import add
>>> result = add.delay(3,5)
>>>
You would see something like below from Celery worker console,
[2014-03-26 15:55:04,117: INFO/MainProcess] Received task: tasks.add[0a52fd72-c7cd-4dc7-91a8-be51f1ff4df2]
[2014-03-26 15:55:04,118: WARNING/Worker-1] hello celery
[2014-03-26 15:55:14,130: INFO/MainProcess] Task tasks.add[0a52fd72-c7cd-4dc7-91a8-be51f1ff4df2] succeeded in 10.0110001564s: 8
If you want to see task status from client, you can use call "result.ready()". However, as we didn't specify the backend for Celery, by defualt it would use "DisabledBackend", you would encounter the following error,
>>> result.ready()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\celery\result.py", line 254, in ready
return self.state in self.backend.READY_STATES
File "C:\Python27\lib\site-packages\celery\result.py", line 390, in state
return self._get_task_meta()['status']
File "C:\Python27\lib\site-packages\celery\result.py", line 327, in _get_task_meta
meta = self.backend.get_task_meta(self.id)
File "C:\Python27\lib\site-packages\celery\backends\base.py", line 291, in get_task_meta
meta = self._get_task_meta_for(task_id)
AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for'
To resolve this issue, here comes the following second senario.
Senario 2: Specify the backend for celery, if we do care about the result
1. Update the module tasks.py to specify parameter "backend" as "amqp". For other backend specification, refer to doc
from __future__ import absolute_import
from celery import Celery
import time app = Celery('tasks', backend="amqp", broker='amqp://guest@localhost:5672//') @app.task
def add(x, y):
print 'hello celery'
time.sleep(10)
return x + y
2. Restart celery worker and open a new python shell. (This is important, otherwise the code update above won't take effect)
3. Test
>>> from tasks import add
>>> result = add.delay(3,5)
>>> result.ready()
False
>>> result.state
'PENDING'
>>> result.status
'SUCCESS'
>>> result.state
'SUCCESS'
>>> result.ready()
True
>>> result.get()
8
>>>
See also: https://denibertovic.com/posts/celery-best-practices/
A simple case to use Celery:的更多相关文章
- Ettus Research USRP B200/B210 simple case
- case when语句后的表达式
SQL中Case When语句的语法如下 Simple CASE expression: CASE input_expression WHEN when_expression THEN result_ ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...
- 楼梯T-SQL:超越基础6级:使用CASE表达式和IIF函数
从他的楼梯到T-SQL DML,Gregory Larsen涵盖了更多的高级方面的T-SQL语言,如子查询. 有时您需要编写一个可以根据另一个表达式的评估返回不同的TSQL表达式的单个TSQL语句. ...
- CASE 表达式
通过本篇文章我们来学习一下CASE表达式的基本使用方法. CASE表达式有简单 CASE表达式(simple case expression)和搜索 CASE表达式(searched caseexpr ...
- SQL进阶随笔--case用法(一)
SQL进阶一整个是根据我看了pdf版本的整理以及自己的见解整理.后期也方便我自己查看和复习. CASE 表达式 CASE 表达式是从 SQL-92 标准开始被引入的.可能因为它是相对较新的技术,所以尽 ...
- A simple Gaussian elimination problem.(hdu4975)网络流+最大流
A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- CASE (Transact-SQL)
A. 使用带有 CASE 简单表达式的 SELECT 语句Using a SELECT statement with a simple CASE expression在 SELECT 语句中,CASE ...
随机推荐
- Jquery attr("checked") 返回checked或undefined 获取选中失效
$('#cb').attr('checked'); 返回的是checked或者是undefined,不是原来的true和false了,有关此问题的解决方法如下: <input type='che ...
- C#.NET常见问题(FAQ)-如何生成release版本的程序,生成debug版本的程序
除了右击项目在生成中配置改成Release还要在顶部切换成Release 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai1 ...
- 霍纳法则(Horner Rule)介绍及C语言实现
参考自:http://flynoi.blog.hexun.com/31272178_d.html 霍纳法则简介 假设有n+2个实数a0,a1,-,an,和x的序列,要对多项式Pn(x)= anxn+a ...
- Stage3d 由浅到深理解AGAL的管线vertex shader和fragment shader || 简易教程 学习心得 AGAL 非常非常好的入门文章
Everyday Stage3D (一) Everyday Stage3D (二) Triangle Everyday Stage3D (三) AGAL的基本概念 Everyday Stage3D ( ...
- eclipse 修改maven项目的jdk版本
eclipse 修改maven项目的jdk版本 CreationTime--2018年6月8日10点29分 Author:Marydon 1.情景展示 jdk版本太低,如何修改 2.错误方式 第一 ...
- linux shutdown命令
shutdown [-t 秒] [-arkhncfF] 时间 [警告信息] 常用选项与参数: -t sec : -t 后面加秒数,亦即『过几秒后关机』的意思 -k : 不要真的关机,只是发送警告信息出 ...
- Drupal Form问题汇总
问:如何校验和提交表单?答:Drupal允许定义默认的表单校验处理函数和提交处理函数. function practice_demo_form($form, &$form_state) { . ...
- java操作hdfs到数据库或者缓存
使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中.不然就没了意义 一下是使用javaAPI操作hdfs存入缓存的代码: <span style="font-fami ...
- eclipse.ini的相关说明
http://www.cnblogs.com/yan5lang/archive/2011/05/24/2055867.htmlEclipse的启动由$ECLIPSE_HOME/eclipse.ini控 ...
- wap站、手机APP 接入支付宝、微信、银联支付。
一.wap站 ①.支付宝接入 1.开发前准备:申请一个通过实名认证的企业支付宝账号,并申请开通手机WAP支付功能. 2.流程 参数准备: 企业支付宝账号的PID(也叫ParnerID)和KEY,如果使 ...