celery是python开发的分布式任务调度模块
Celery本身不含消息服务,它使用第三方消息服务来传递任务,目前,celery支持的消息服务有RabbitMQ,redis甚至是数据库,redis是最佳选择
已安装配置好环境python3.x
已成功安装django
1.安装依赖包:
cmd下执行命令:
...>pip3 install celery
...>pip3 install redis
...>pip3 install django-celery
...>pip3 install celery-with-redis
2.django的项目目录下的setting.py中:
INSTALLED_APPS中添加内容:
INSTALLED_APPS = [ ...... ‘djcelery‘, ]
添加配置信息:
import djcelery
djcelery.setup_loader() # 加载djcelery
# 数据库调度
CELERYBEAT_SCHEDULER=‘djcelery.schedulers.DatabaseScheduler‘
BROKER_URL=‘redis://127.0.0.1:6379/0‘
BROKER_TRANSPORT=‘redis‘
3.验证配置成功:
应用目录下创建celery.py:
from __future__ import absolute_import import os import django from celery import Celery from django.conf import settings os.environ.setdefault(‘DJANGO_SETTINGS_MODULES‘,‘autotest.settings‘) django.setup() app=Celery(‘autotest‘) app.config_from_object(‘django.conf:settings‘) app.autodiscover_tasks(lambda:settings.INSTALLED_APPS)
应用目录下创建tasks.py:
from apitest.celery import app @app.task def hello(): print(‘已运行‘)
cmd下项目manage.py目录执行命令:
...>python3 manage.py celeryd -l info
...>python3 manage.py runserver
浏览器访问:
http://127.0.0.1:8000/admin/djcelery/
如图配置成功:
原文地址:https://www.cnblogs.com/reyinever/p/10949934.html
时间: 2024-10-07 15:48:40