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 ...
随机推荐
- ArcGIS中的批量处理
在实际生产过程中,经常遇到批量处理数据的情况.在ArcGIS中,除自己写代码来处理这类问题外,它提供了一个批量处理的工具,在ToolBox对应的工具上右键即可选择批处理工具. 和单个处理方式一样,输入 ...
- Windows改桌面文件路径
默认的桌面和用户文件都是C盘,每次重装系统要备份,为了方便可以把它设置到其他盘符,一种方式是通过一些软件功能,如360有一个C盘搬家,也可以修改注册表文件: Windows Registry Edit ...
- ora01219数据库未打开
今天连接数据后,一看提示ora01219数据库未打开,关了服务重开仍然是这样,在度娘找了下才发现问题 应该是我删除了一个数据文件,看下解决办法 错误原因: 直接关闭数据库,然后删除DBF文件.即表空间 ...
- MySQL 分库备份
mysql -uroot -p'password' -e "show databases;"|grep -Evi "database|infor|perfor" ...
- jersey获取各个参数的总结
service端: @Path("/hello") public class HelloService { @GET @Produces("text/plain" ...
- Android 沉浸式状态栏的三种实现方式
沉浸式状态栏 Google从android kitkat(Android 4.4)開始,给我们开发人员提供了一套能透明的系统ui样式给状态栏和导航栏,这种话就不用向曾经那样每天面对着黑乎乎的上下两条黑 ...
- 开源大数据技术专场(下午):Databircks、Intel、阿里、梨视频的技术实践
摘要: 本论坛第一次聚集阿里Hadoop.Spark.Hbase.Jtorm各领域的技术专家,讲述Hadoop生态的过去现在未来及阿里在Hadoop大生态领域的实践与探索. 开源大数据技术专场下午场在 ...
- aop注解 spring提供的事务
http://www.cnblogs.com/friends-wf/p/3826893.html 是 自定义的切面,并且添加注解 声明为切面 利用spring提供的事务声明 主要在 service层上 ...
- SQLAlchemy基本使用(Flask中)
SQLAlchemy介绍 SQLAlchemy是一个基于Python实现的ORM框架. 该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用数据 ...
- HDUOJ----4004The Frog's Games(二分+简单贪心)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...