# _*_ coding: utf-8 _*_
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celerysy.settings') # 设置django环境
app = Celery('celerysy')
app.config_from_object('django.conf:settings', namespace='CELERY') # 使用CELERY_ 作为前缀,在settings中写配置
# app.autodiscover_tasks() # 发现任务文件每个app下的task.py
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
epgapp目录下创建tasks.py
from __future__ import absolute_import, unicode_literals
__author__ = 'admin'
__date__ = '2019/8/29 10:29'
from celery import shared_task
import time
@shared_task
def add(x, y):
print(time.time())
return 1
@shared_task
def mul(x, y):
return x * y
@shared_task
def xsum():
print("1313131351")
整体目录结构
启动redis,启动celery
celery -A celerysy beat -l info -S django //通过admin后台管理的任务
celery -A celerysy beat -l info //执行通过settings配置的任务
celery -A celerysy worker -P eventlet //后台查看任务输出情况