首页 > 代码库 > django+celery配置(定时任务)

django+celery配置(定时任务)

下面介绍一下django+celery的配置做定时任务

1.首先介绍一下环境和版本

   python==2.7

 django == 1.8.1

   celery == 3.1.23

   django-celery == 3.1.17

2.celery的安装

  sudo pip install celery==3.1.23

  sudo pip install django-celery==3.1.17

3.新建一个项目

  (1)django-admin startproject django_celery_demo

      (2)cd django_celery_demo

      (3)django-admin startapp appdemo

4.修改项目配置(settings.py)

    (1)引入absolute_import

from __future__ import absolute_import

    (2)INSTALLED_APPS添加‘djcelery‘,‘appdemo‘

INSTALLED_APPS = (
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
    djcelery,
    appdemo,
)

    (3)配置celery

import djcelery
from celery.schedules import crontab
djcelery.setup_loader()
BROKER_URL = redis://:dahai123@192.168.5.60:6380/6

from datetime import timedelta


CELERYBEAT_SCHEDULE = {
    add-every-30-seconds: {
        task: appdemo.tasks.test_celery,
        # ‘schedule‘: crontab(minute=u‘40‘, hour=u‘17‘,),
        schedule: timedelta(seconds=3),
        args: (16, 16)
    },
}

 未完待续

django+celery配置(定时任务)