Celery是由Python开发的一个简单、灵活、可靠的处理大量任务的分发系统,它不仅支持实时处理也支持任务调度。
- user:用户程序,用于告知celery去执行一个任务。
- broker: 存放任务(依赖RabbitMQ或Redis,进行存储)
- worker:执行任务
celery需要rabbitMQ、Redis、Amazon SQS、Zookeeper(测试中) 充当broker来进行消息的接收,并且也支持多个broker和worker来实现高可用和分布式。http://docs.celeryproject.org/en/latest/getting-started/brokers/index.html
Celery version 4.0 runs on Python ❨2.7, 3.4, 3.5❩ PyPy ❨5.4, 5.5❩ This is the last version to support Python 2.7, and from the next version (Celery 5.x) Python 3.5 or newer is required.If you’re running an older version of Python, you need to be running an older version of Celery: Python </span>2.6: Celery series 3.1 <span style="color: #0000ff;">or</span><span style="color: #000000;"> earlier. Python </span>2.5: Celery series 3.0 <span style="color: #0000ff;">or</span><span style="color: #000000;"> earlier. Python </span>2.4 was Celery series 2.2 <span style="color: #0000ff;">or</span><span style="color: #000000;"> earlier. Celery </span><span style="color: #0000ff;">is</span> a project with minimal funding, so we don’t support Microsoft Windows. Please don’t open any issues related to that platform.</pre>
环境准备:
- 安装rabbitMQ或Redis
见:http://www.cnblogs.com/wupeiqi/articles/5132791.html - 安装celery
pip3 install celery
快速上手
import time from celery import Celeryapp = Celery('tasks', broker='redis://192.168.10.48:6379', backend='redis://192.168.10.48:6379')
@app.task
def xxxxxx(x, y):
time.sleep(10)
return x + y
#!/usr/bin/env python # -*- coding:utf-8 -*- from s1 import xxxxxx# 立即告知celery去执行xxxxxx任务,并传入两个参数
result = xxxxxx.delay(4, 4)
print(result.id)