Celery分布式任务队列

Celery分布式任务队列

Choosing a Broker

RabbitMQ

Redis

Databbase(SQLAlchemy/Django Database)

Create app

1
2
3
4
5
6
7
from celery import Celery
app = Celery('tasks', broker='amqp://guest@localhost//')
@app.task
def add(x, y):
return x + y

Create Celery Server

1
celery -A tasks worker --loglevel=info

Call APP

1
2
>>> from tasks import add
>>> add.delay(4, 4)

Results

如果你想保存结果

1
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

Task

通过使用装饰器task(),就可以创建一个task了.

names

一种好的实践模式是使用模块名字当作人物名

1
2
3
>>> @app.task(name='tasks.add')
>>> def add(x, y):
... return x + y

Context

request contains information and state related to the executing task.
request的属性

Custom states

1
2
3
4
5
6
@app.task(bind=True)
def upload_files(self, filenames):
for i, file in enumerate(filenames):
if not self.request.called_directly:
self.update_state(state='PROGRESS',
meta={'current': i, 'total': len(filenames)})

Designing Workflows

坚持技术分享,您的支持将鼓励我继续创作!