码迷,mamicode.com
首页 > 其他好文 > 详细

django 2.x + celery 4.2.x 配置文件 设置

时间:2018-11-28 13:16:39      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:sel   discover   zone   .config   back   shanghai   xxxxxx   scheduler   bin   

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 配置文件 设置

标签:sel   discover   zone   .config   back   shanghai   xxxxxx   scheduler   bin   

原文地址:http://blog.51cto.com/hequan/2323058

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!