安装celery、 django_celery_beat 和 django_celery_result ,redis安装配置略
相比普通django工程,工程里面新增py文件celery ,App里面新增py文件tasks,此处文件名必须是tasks
编辑celery.py文件
from __future__ import absolute_import,unicode_literals
import os
from celery import Celery,platforms
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celeryProject.settings')
broker = 'redis://localhost:6379/0'
app = Celery('celeryProject',broker=broker)
# app.conf.timezone = 'Asia/Shanghai'
app.config_from_object('django.conf:settings',namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
platforms.C_FORCE_ROOT = True
@app.task(bind=True)
def debug_task(self):
print('Request:{0!r}'.format(self.request))
from __future__ import absolute_import,unicode_literals
from celery import shared_task
@shared_task
def add(x,y):
print("1+1")
return x+y
@shared_task
def sub(x,y):
print("1-1")
return x-y