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
USE_TZ = True
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/2'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERYD_CONCURRENCY = 10
CELERY_TIMEZONE = 'Asia/Shanghai'
CELERYD_MAX_TASKS_PER_CHILD = 20
CELERYD_FORCE_EXECV = True
CELERY_IGNORE_RESULT = True
CELERY_CREATE_MISSING_QUEUES = True
CELERYD_TASK_SOFT_TIME_LIMIT = 600
CELERY_TASK_RESULT_EXPIRES = 600
CELERY_ENABLE_UTC = False
任务
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