celery笔记

2017-05-28  本文已影响0人  橄榄树下的托马斯

作者:刘宾, thomas_liub@hotmail.com
请尊重作者著作权,转载请注明出处,谢谢!


分布式任务队列,配合Django扩展Django异步处理能力。参考资料

1. 安装celery, 并建立Celery文件

pip install celery

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# 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 don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


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

2. Django初始化加载

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

3. 添加任务

# Create your tasks here
from __future__ import absolute_import, unicode_literals
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)

4. 启动worker

celery -A proj worker -l info

5. 周期性调度

1. 安装celery-beat

pip install django-celery-beat

2. 安装app

INSTALLED_APPS = (
        ...,
        'django_celery_beat',
    )

3. 同步数据库表

python manage.py migrate

4. 启动

celery -A proj beat -l info -S django

上一篇 下一篇

猜你喜欢

热点阅读