前言
celery是一个基于python开发的简单、灵活且可靠的分布式任务队列框架,支持使用任务队列的方式在分布式的机器/进程/线程上执行任务调度。在Django中主要处理异步和定时任务。
配置
# file proj/proj/celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
# file proj/proj/__init__.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
# 启动work
celery -A proj worker -l info
# 定时任务
celery -A myproject beat -l info
异步任务
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task
def add(x, y):
return x + y
def common_views(request):
add.delay() # 调用异步任务
return
定时任务
from celery.task.schedules import crontab
from celery.decorators import periodic_task
@periodic_task(run_every=crontab(minute='*', hour='*', day_of_week='*', day_of_month='*', month_of_year='*'))
def some_task():
print('periodic task test!!!!!')
time.sleep(5)
print('success')
return True
# 或
from celery.schedules import crontab
app.conf.update(
CELERYBEAT_SCHEDULE = {
'sum-task': {
'task': 'deploy.tasks.add',
'schedule': timedelta(seconds=20),
'args': (5, 6)
}
'send-report': {
'task': 'deploy.tasks.report',
'schedule': crontab(hour=4, minute=30, day_of_week=1),
}
}
)
参考
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
http://yshblog.com/blog/164
https://juejin.im/post/5b588b8c6fb9a04f834655a6