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:的更多相关文章

  1. Ettus Research USRP B200/B210 simple case

  2. case when语句后的表达式

    SQL中Case When语句的语法如下 Simple CASE expression: CASE input_expression WHEN when_expression THEN result_ ...

  3. hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)

    这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...

  4. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  5. 楼梯T-SQL:超越基础6级:使用CASE表达式和IIF函数

     从他的楼梯到T-SQL DML,Gregory Larsen涵盖了更多的高级方面的T-SQL语言,如子查询. 有时您需要编写一个可以根据另一个表达式的评估返回不同的TSQL表达式的单个TSQL语句. ...

  6. CASE 表达式

    通过本篇文章我们来学习一下CASE表达式的基本使用方法. CASE表达式有简单 CASE表达式(simple case expression)和搜索 CASE表达式(searched caseexpr ...

  7. SQL进阶随笔--case用法(一)

    SQL进阶一整个是根据我看了pdf版本的整理以及自己的见解整理.后期也方便我自己查看和复习. CASE 表达式 CASE 表达式是从 SQL-92 标准开始被引入的.可能因为它是相对较新的技术,所以尽 ...

  8. A simple Gaussian elimination problem.(hdu4975)网络流+最大流

    A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...

  9. CASE (Transact-SQL)

    A. 使用带有 CASE 简单表达式的 SELECT 语句Using a SELECT statement with a simple CASE expression在 SELECT 语句中,CASE ...

随机推荐

  1. java编程思想 第四版 第六章 个人练习

    欢迎加群:239063848 进群须知:本群仅用于技术分享与交流.问题公布与解答 禁止闲聊.非诚勿扰 练习1:(1)在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例. import mi ...

  2. android 如何连真机测试

    1. 设置android手机为USB调试模式.步骤: menu---> 设置 ---> 应用程序 ---> 开发 , 选择[USB调试] 2. 用USB连接手机和电脑,并确保成功.步 ...

  3. android studio中使用adb wifi插件无线调试程序

    使用android studio中使用adb wifi插件无线调试程序的前提条件电脑和手机在同一个无线网 1.下载adb wifi插件 File->Settings->Plugins Br ...

  4. Adobe Dynamic Http Streaming的简单配置与实现 (FMS, HLS, HDS)

    http://blog.csdn.net/avsuper/article/details/7663879 Adobe的Http Dynamic Streaming是针对苹果的HLS方案提出基于HTTP ...

  5. android 随手记 读写文件的几种方式

    java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 */ import java.io.BufferedRe ...

  6. 微信小程序 - 文字收缩与展开

    wxml <view class='containers'> <text class="content {{ellipsis?'ellipsis':'unellipsis' ...

  7. php之快速入门学习-9(switch)

    PHP Switch 语句 switch 语句用于根据多个不同条件执行不同动作. PHP Switch 语句 如果您希望有选择地执行若干代码块之一,请使用 switch 语句. <?php sw ...

  8. Java从零开始学五(数据类型转换)

    一.数据类型转换 分为“自动类型转换”和“强制类型转换” 二.自动类型转换 低级别------>高级别 byte b=7; int i=b; System.out.println("i ...

  9. 子查询三(在FROM子句中使用子查询)

    FROM子句中使用子查询一般都是返回多行多列,可以将其当作一张数据表 示例一.查询出每个部门的编号,名称,位置,部门人数,平均工资 SELECT d.deptno,d.dname,d.loc,temp ...

  10. Linux的网卡由eth0变成了eth1怎么办?

    Linux的网卡由eth0变成了eth1怎么办? Linux的网卡由eth0变成了eth1,如何修复 使用wmware安装了linux,安装成功后,使用的网卡是eth0,没有eth1.但是用过一段时间 ...