Django框架17: Celery的使用

Celery介绍html

1.什么是Celery
    Celery是一个python模块,它在官网的定义:Celery is asynchronous task queue/job based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. 
    这里强调的概念包括:异步任务队列,分布式消息传递,实时或调度任务。python

2.为何要使用Celery
    Celery是一个使用Python开发的分布式任务调度模块,所以对于大量使用Python构建的系统,能够说是无缝衔接,使用起来很方便。Celery专一于实时处理任务,同时也支持任务的定时调度。所以适合实时异步任务定时任务等调度场景。git

3.Celery任务队列
    任务队列是一种在进程或机器之间分发任务的机制。  
    任务队列的输入是被称为任务(task)的工做单元。专用的工做进程会时刻监控任务队列,来获取要执行的任务。 
    celery的client和worker经过消息来“沟通“。Celery须要依靠RabbitMQ等做为消息代理,同时也支持Redis甚至是Mysql,Mongo等,固然,官方默认推荐的是RabbitMQ。
    为了开始一个任务,client须要向队列中发送任务消息,而后broker会把任务投递给worker处理。一个celery系统能够包含多个worker和broker,以便实现高可用和可水平扩展。github

4.官方网站:http://www.celeryproject.org/redis

5.中文文档:http://docs.jinkan.org/docs/celery/sql

Celery配置shell

参考Github实践例子:https://github.com/celery/celery/tree/3.1/examples/djangodjango

1.在包含settings文件夹proj的__init__.py中添加json

# proj/__init__.py
from __future__ import absolute_import
from .celery import app as celery_app

2.一样在该文件夹中建立celery.py文件。bash

# proj/celery.py
# coding:utf8
from __future__ import absolute_import

import os

from celery import Celery
from django.conf import settings

# set the default Django settings module for the 'celery' program.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

3.在settings.py文件中,添加一些celery的配置。

# 修改点1
INSTALLED_APP += [
    'kombu.transport.django',
    'djcelery'
]

# 修改点2
# 生产环境我使用redis做为broker
BROKER_URL = 'redis://172.23.18.116:6379/0'
# 开发环境能够直接用django做为broker
BROKER_URL = 'django://'

# 修改点3
#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

4.作好上面的步骤以后,就能够在本身的app中新增tasks.py文件,里面用shared_task来装饰celery任务,如demoapp/tasks.py。

from __future__ import absolute_import
from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

celery测试及使用

1.首先启动worker。

# 这里指定了配置,默认的话忽略info后面
python manage.py celery worker --loglevel=info --settings=proj.settings

2.使用下面的命令也就是在django环境中执行,经常使用来测试django模块功能。

# 同上忽略指定的配置文件
python manage.py shell --settings=proj.settings

    试着执行shared_task add()。

from demoapp.tasks import add
add.delay(3, 3)

    上面的worker log中出现add()的return也就是6,说明celery配置正确。

3.django-celery的定时任务功能
    这一步是celery beat定时或者定间隔给celery发送task。

# proj/settings.py
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'12

    接下来,将目标djcelery的定时文件迁移一下,便可在admin后台配置定时任务时间间隔等内容。

python manage.py migrate

    再而后

python manage.py celery beat

    能够看到celery beat开始按以前admin后台设置的时间间隔或者crontab开始发送task给celery了,此时天然也须要celery worker在running状态。celery beat发送的任务return的结果在celery log中能够看到,说明django-celery 定时任务配置成功。

原文出处:https://www.jzfblog.com/detail/115,文章的更新编辑以此连接为准。欢迎关注源站文章!

相关文章
相关标签/搜索