标签:时间表 序列化 trace star with ring last page space
一、安装
pip install celery
pip install django-celery-beat
pip install django-celery-results
pip install redis
安装这4个库
请注意不是django-celery、django-redis、celery-with-redis等库
以前版本的Celery需要一个单独的库来与Django一起工作
但是自从3.1以后就不再是这样了
Django现在支持开箱即用
pip list
celery==4.3.0
django-celery-beat==1.5.0
django-celery-results==1.1.2
redis==3.2.1
二、配置Celery
1、在项目demo/settings.py文件下加入:
‘django_celery_beat‘,
# django-celery-beat
‘django_celery_results‘,
# django-celery-results
CELERY_ENABLE_UTC = False
# 不使用国际标准时间
CELERY_TIMEZONE = ‘Asia/Shanghai‘
# 使用亚洲/上海时区
DJANGO_CELERY_BEAT_TZ_AWARE = False
# 解决时区问题
CELERY_BROKER_URL = ‘redis://:Abcdef@123456@192.168.1.102:6379/0‘
# redis://:password@hostname:port/db_number
CELERY_BROKER_TRANSPORT = ‘redis‘
# 使用redis作为中间件
CELERY_BEAT_SCHEDULER = ‘django_celery_beat.schedulers:DatabaseScheduler‘
# 自定义调度类,使用Django的ORM
CELERY_RESULT_BACKEND = ‘django-db‘
# 任务结果,使用Django的ORM
CELERY_ACCEPT_CONTENT = [‘application/json‘]
# 设置任务接收的序列化类型
CELERY_TASK_SERIALIZER = ‘json‘
# 设置任务序列化方式
CELERY_RESULT_SERIALIZER = ‘json‘
# 设置结果序列化方式
2、在项目demo目录下新建celery.py文件:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery, platforms
from django.utils.datetime_safe import datetime
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘demo.settings‘)
# 设置默认celery命令行的环境变量
app = Celery(‘demo‘)
# 实例化celery
app.now = datetime.now
# 解决时区问题
app.config_from_object(‘django.conf:settings‘, namespace=‘CELERY‘)
# 直接从Django设置中配置Celery
app.autodiscover_tasks()
# 从所有应用中加载任务模块tasks.py
platforms.C_FORCE_ROOT = True
# 解决celery不能root用户启动的问题
3、在项目demo/__init__.py文件加入:
(请注意不是应用的__init__.py)
from __future__ import absolute_import, unicode_literals
from demo.celery import app as celery_app
import pymysql
__all__ = (‘celery_app‘,)
pymysql.install_as_MySQLdb()
4、在应用product目录下面新建tasks.py文件:
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from demo.celery import app
@app.task
def debug_1():
print("调试_1已运行!")
@app.task
def debug_2():
print("调试_2已运行!")
5、在应用product目录下面product/admin.py文件加入:
from django_celery_beat.models import IntervalSchedule, CrontabSchedule, ClockedSchedule, SolarSchedule, PeriodicTask
from django_celery_results.models import TaskResult
class IntervalScheduleAdmin(object):
list_display = [
‘id‘, ‘every‘, ‘period‘,
]
ordering = [‘id‘]
search_fields = [‘every‘]
list_per_page = 10
class CrontabScheduleAdmin(object):
list_display = [
‘id‘, ‘minute‘, ‘hour‘,
‘day_of_week‘, ‘day_of_month‘, ‘month_of_year‘, ‘timezone‘
]
ordering = [‘id‘]
search_fields = [‘minute‘]
list_per_page = 10
class SolarScheduleAdmin(object):
list_display = [
‘id‘, ‘event‘, ‘latitude‘, ‘longitude‘
]
ordering = [‘id‘]
search_fields = [‘event‘]
list_per_page = 10
class ClockedScheduleAdmin(object):
list_display = [
‘id‘, ‘clocked_time‘, ‘enabled‘
]
ordering = [‘id‘]
search_fields = [‘clocked_time‘]
list_per_page = 10
class PeriodicTaskAdmin(object):
list_display = [
‘id‘, ‘name‘, ‘task‘, ‘args‘, ‘kwargs‘, ‘queue‘,
‘exchange‘, ‘routing_key‘, ‘expires‘, ‘enabled‘,
‘last_run_at‘, ‘total_run_count‘, ‘date_changed‘, ‘description‘,
‘interval‘, ‘crontab‘, ‘solar‘, ‘clocked‘, ‘one_off‘,
‘start_time‘, ‘priority‘, ‘headers‘
]
ordering = [‘id‘]
search_fields = [‘name‘]
list_per_page = 10
class TaskResultAdmin(object):
list_display = [
‘id‘, ‘task_id‘, ‘status‘, ‘content_type‘, ‘content_encoding‘,
‘result‘, ‘date_done‘, ‘traceback‘, ‘hidden‘, ‘meta‘,
‘task_args‘, ‘task_kwargs‘, ‘task_name‘
]
ordering = [‘id‘]
search_fields = [‘task_id‘]
list_per_page = 10
xadmin.site.register(IntervalSchedule, IntervalScheduleAdmin)
# 间隔任务
xadmin.site.register(CrontabSchedule, CrontabScheduleAdmin)
# 定时任务
xadmin.site.register(SolarSchedule, SolarScheduleAdmin)
# 太阳时间表
xadmin.site.register(ClockedSchedule, ClockedScheduleAdmin)
# 计时时间表
xadmin.site.register(PeriodicTask, PeriodicTaskAdmin)
# 配置任务
xadmin.site.register(TaskResult, TaskResultAdmin)
# 任务结果
三、迁移数据库
(没有静态资源文件,也不用激活模型)
python manage.py migrate
可以看到数据库新增了这几张表
四、Web后台
五、启动Celery
开2个终端到项目的根目录下分别执行
celery -A demo worker -l info
启动任务执行单元Worker
celery -A demo beat -l info
启动定时任务
控制台日志
Web后台
标签:时间表 序列化 trace star with ring last page space
原文地址:https://www.cnblogs.com/yjlch1016/p/11625471.html