1. Environment
python==2.7
djang==1.11.2 # 1.8, 1.9, 1.10应该都没问题
celery-with-redis==3.0 # 需要用到redis作为中间人服务(Broker)
celery==3.1.25 # 安装上面的会自动安装
kombu==3.0.37
billiard==3.3.0.23
django-celery==3.2.2 # celery插件, 实现定时任务
Celery>=4.0 does not support windows, please refer to the official documentation for details
2. Installation
pip install django==1.11.2 celery-with-redis==3.0 django-celery==3.2.2
3. Install Redis as a Broker (officially recommended by RabbitMQ, but troublesome to install)
教程很多, 略
4. Create a new django project
- Demo
- Demo
setting.py
wsgi.py
urls.py
- app
- migrations
models.py
views.py
...
- configure
settings.py
INSTALLED_APPS = (
...
'app',
'djcelery',# django-celery 可以在admin后台定义定时任务, 开始前需要直接migrate
)
BROKER_URL = 'redis://127.0.0.1:6379/0'
BROKER_TRANSPORT = 'redis'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler' # 数据库调度
- create a new file
Demo/Demo/celery.py
from __future__ import absolute_import
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Demo.settings')
from django.conf import settings # noqa
app = Celery('Demo')
# 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))
- new
Demo/app/tasks.py
from Demo.celery import app
@app.task
def cus_task(*arg):
print('This is a test task')
- 编辑
Demo/app/views.py
```
from django.shortcuts import render, HttpResponse
from .tasks import cus_task
def index(request):
cus_task.delay()
return HttpResponse("Test async task")
- 启动`django`和`celery`
django
python manage.py runserver
celery
celery -A Demo worker -l debug
#### admin后台中配置`celery`计划
- 配置
The settings in settings.py above, and then execute
python manage.py migrate djcelery
- 登陆admin后台进行配置
List of Djcelery modules
Crontabs # Same as linux crontab
Intervals # Interval
Periodic tasks # Periodic tasks
Tasks
Workers
> 配置一个`periodic task`任务内容 `app.tasks.cus_task` 用`crontab`或`interval`设置每5s执行一次
- 启动django和celery, 并查看日志
celery -A Demo worker -l debug
another window
celery -A Demo beat -l debug --max-interval=10 # Scan djcelery tasks every 10s
### 补充
- [ ] 安装`flower`
pip install flower
- [ ] 启动flower(默认启动一个webserver:5555)
python manage.py celery flower
```
- [ ] Open the browser to
http://localhost:5555
view details