django 2.x + celery 4.2.x 配置文件 设置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 2.x + celery 4.2.x 配置文件 设置相关的知识,希望对你有一定的参考价值。
django 2.x + celery 4.2.x配置
模块
celery==4.2.1
django-celery-beat==1.1.1
django-celery-results==1.0.1
kombu==4.2.1
配置
__init__.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = [‘celery_app‘]
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery.schedules import crontab
from datetime import timedelta
from celery import Celery, platforms
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘xxxxxx.settings‘)
app = Celery(‘xxxxxxxx‘)
app.config_from_object(‘django.conf:settings‘, namespace=‘CELERY‘)
app.autodiscover_tasks()
platforms.C_FORCE_ROOT = True
@app.task(bind=True)
def debug_task(self):
print(‘Request: {0!r}‘.format(self.request))
app.conf.update(
CELERYBEAT_SCHEDULE={
‘city‘: {
‘task‘: ‘city.tasks.city_task‘,
‘schedule‘: timedelta(hours=1),
‘args‘: ‘‘
},
}
)
settings.py
INSTALLED_APPS = [
‘django_celery_results‘,
‘django_celery_beat‘,
]
TIME_ZONE = ‘Asia/Shanghai‘
USE_I18N = True
USE_L10N = False # 注意是False 配合下边时间格式
USE_TZ = False # 如果只是内部使用的系统,这行建议为false,不然会有时区问题
DATETIME_FORMAT = ‘Y-m-d H:i:s‘
DATE_FORMAT = ‘Y-m-d‘
CELERY_RESULT_BACKEND = ‘redis://localhost:6379/1‘
# CELERY_RESULT_BACKEND = ‘django-db‘
CELERY_BROKER_URL = ‘redis://localhost:6379/0‘
CELERYBEAT_SCHEDULER = ‘django_celery_beat.schedulers:DatabaseScheduler‘
CELERYD_CONCURRENCY = 20
CELERY_TIMEZONE = ‘Asia/Shanghai‘
CELERYD_MAX_TASKS_PER_CHILD = 30
CELERYD_FORCE_EXECV = True
CELERY_IGNORE_RESULT = True
CELERY_CREATE_MISSING_QUEUES = True
CELERYD_TASK_SOFT_TIME_LIMIT = 1200
CELERY_TASK_RESULT_EXPIRES = 1200
任务
city/
tasks.py
import logging
import json
import os
import configparser
import string
from celery import shared_task
import datetime
logger = logging.getLogger(‘celery‘)
@shared_task
def city_task():
pass
生成表结构,启动redis
启动
celery -B -A argus worker -l info
以上是关于django 2.x + celery 4.2.x 配置文件 设置的主要内容,如果未能解决你的问题,请参考以下文章
django+django-celery+celery的整合实战