参考这篇文章:http://my.oschina.net/u/2306127/blog/417360
由于选择RabbitMQ作为消息中间件,先安装运行RabbitMQ,详见http://blog.csdn.net/mirahs/article/details/48496611
安装celery
pip install celery
编写一个简单的任务,tasks.py
from celery import Celery
app = Celery('tasks', backend='amqp', broker='amqp://guest@localhost//')
app.conf.CELERY_RESULT_BACKEND = 'amqp://'
@app.task
def add(x, y):
return x + y
在当前目录运行一个worker,用来执行这个tasks
[root@test celery]# export C_FORCE_ROOT=/root
[root@test celery]# celery -A tasks worker --loglevel=info
客户端代码,test_tasks.py
from tasks import add
import time
result = add.delay(4, 4)
print 'waiting result...'
while not result.ready():
time.sleep(2)
print 'Result: ', result.get()
执行测试
[root@test celery]# python test_tasks.py
waiting result...
Result: 8