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/

最新文章

  1. 兼容IE浏览器的js浏览器全屏代码
  2. db2+python+sqlchemy环境的搭建
  3. Android 使用shape来画线
  4. [NetTopologySuite](2)任意多边形求交
  5. Dropbox能火,为何它的中国同行不能火?
  6. Topcoder SRM 597
  7. logstash5.x改变
  8. 示例可重用的web component方式组织angular应用模块
  9. JdbcTemplate 、NamedParameterJdbcTemplate、SimpleJdbcTemplate的区别
  10. cocos2dx中的用户数据的管理
  11. 【WPF】逻辑树和视觉树
  12. Json 的日期格式转换成DateTime
  13. 【转】三星8552 手机提示升级系统 完成后重启 开机画面一直停留在三星的LOGO 一闪一闪 怎么办
  14. 理解FMS中的实例
  15. 2016-2017 CT S03E06: Codeforces Trainings Season 3 Episode 6 The Baguette Master
  16. [Pelican]Pelican入门(一)
  17. 腾讯2017年暑期实习生编程题【算法基础-字符移位】(C++,Python)
  18. 【高速接口-RapidIO】3、RapidIO串行物理层的包传输过程
  19. PHP 爬虫——QueryList
  20. 数据分析工具R和RStudio入门介绍

热门文章

  1. Android动态加入控件约束位置
  2. 87. 再谈变体型Variant
  3. 使用curl在命令行中下载文件
  4. 虚拟机下linux迁移造成MAC地址异常处理办法
  5. libevent个人理解
  6. EXCEPTION-SPRING
  7. 〖Linux〗使用gsoap搭建web server(C++)
  8. 【Oracle】事务处理
  9. python之函数用法bin()
  10. 头文件dirent.h