1. 程式人生 > >django 2.x + celery 4.2.x 配置檔案 設定

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('arugs')
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