Celery 快速入门
任务队列
任务队列用于分发工作给不同线程或机器。
Celery通过消息传递
支持多个workers和brokers。提供高可用和水平扩展性。
用Python写的
优点
- 简单
- 高可用
- 快
- 易扩展
支持
- Brokers
- RabbitMQ, Redis
- MongoDB, ZeroMQ
- CouchDB, SQLAlchemy
- Django ORM, Amazon SQS,
- ...
- Result Stores
- AMQP, Redis
- memcached, MongoDB
- SQLAlchemy, Django ORM
- Apache Cassandra
- Concurrency
- prefork
- Eventlet, gevent
- threads/single threaded
- Serialization
- pickle, json, yaml, msgpack.
- zlib, bzip2 compression.
- Cryptographic message signing
安装
pip install celery
pip install beanstalkc
选择Broker
$ sudo apt-get install rabbitmq-server
设置Broker
$ sudo rabbitmqctl add_user myuser mypassword
$ sudo rabbitmqctl add_vhost myvhost
$ sudo rabbitmqctl set_permissions -p myvhost myuser ".*" ".*" ".*"
应用
首先你要创建Celery实例。
from celery import Celery
BROKER_URL = ‘amqp://guest:[email protected]:5672//‘
app = Celery(‘tasks‘, broker=BROKER_URL)
@app.task
def add(x, y):
return x + y
执行celery worker服务器
$ celery -A tasks worker --loglevel=info
通过使用supervisord守护进程执行
调用task
>>> from tasks import add
>>> add.delay(4, 4)
取得结果
app = Celery(‘tasks‘, backend=‘amqp‘, broker=‘amqp://‘)
>>> result = add.delay(4, 4)
>>> result.ready()
True
>>> result.get(timeout=1)
8
More
Celery 快速入门
时间: 2024-10-31 06:56:19