django+celery配置异步和定时任务

安装

pip3 install celery
pip3 install django-celery
pip3 install flower

项目目录

celery.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery, platforms

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "celery_test.settings")

from django.conf import settings  # noqa

app = Celery('django_celery_demo')
platforms.C_FORCE_ROOT = True

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

celery_test/init.py

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

settings.py

在 INSTALLED_APPS 里添加'djcelery'

    ...
     'app01',
    'djcelery'
LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False
from datetime import timedelta

BROKER_URL= 'redis://127.0.0.1:6379/4'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/4'

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

}

app01/tasks.py

每个app下新建名称叫tasks.py的文件,文件名必须叫tasks.py,因为app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)其实就是去所有app下去寻找tasks.py里被task装饰的任务

from __future__ import absolute_import
from celery import task
import time

@task
def add(x, y):
    time.sleep(30)    #模拟长时间执行
    return x + y

views.py

from django.http import HttpResponse
from app01.tasks import add
# Create your views here.
def asynch(request, *args, **kwargs):
    add.delay(1,2)
    # 立即返回,不会有任何延迟
    return HttpResponse('hello')

启动

启动前先要创建表。
python3 manage.py runserver 0.0.0.0:9001 -- 启动django
python3 manage.py celery worker -l info -- 启动worker
要想跑循环任务或定时任务:在celery_test 的目录下执行 celery -A celery_test beat -l info
监控worker进程:python3 manage.py celery flower --address=0.0.0.0 --port=5555 需要安装flower: pip3 install flower

原文地址:https://www.cnblogs.com/longyunfeigu/p/9554067.html