时间:2021-05-22
安装依赖
pip install django-celery-beatpip install django-celery-emailpip install celerypip install msgpack-pythonpip install msgpack在settings文件中配置
添加app应用到installed_apps中
"djcelery_email", "django_celery_beat"修改.env文件配置:
#邮箱后端,使用celery emailDJANGO_EMAIL_BACKEND=djcelery_email.backends.CeleryEmailBackendDJANGO_EMAIL_HOST=smtp.qq.comDJANGO_EMAIL_HOST_USER=xxx@qq.com # 您的邮箱号DJANGO_EMAIL_HOST_PASSWORD=你的邮箱密码DJANGO_DEFAULT_FROM_EMAIL=默认发送的邮箱号#CELERYCELERY_BROKER_URL=redis://127.0.0.1:6379/1 #使用redis 1作为消息代理CELERY_RESULT_BACKEND=redis://127.0.0.1:6379/2 #把任务结果放在redis2库中添加配置项:
# Celery# ------------------------------------------------------------------------------if USE_TZ: # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-timezone CELERY_TIMEZONE = TIME_ZONE# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-broker_urlCELERY_BROKER_URL = env("CELERY_BROKER_URL")# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_backendCELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND')# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-accept_contentCELERY_ACCEPT_CONTENT = ["json", 'msgpack']# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-task_serializerCELERY_TASK_SERIALIZER = "msgpack"# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_serializerCELERY_RESULT_SERIALIZER = "json"# http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-time-limit# TODO: set to whatever value is adequate in your circumstancesCELERY_TASK_TIME_LIMIT = 5 * 60# http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-soft-time-limit# TODO: set to whatever value is adequate in your circumstancesCELERY_TASK_SOFT_TIME_LIMIT = 60# http://docs.celeryproject.org/en/latest/userguide/configuration.html#beat-schedulerCELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"EMAIL_BACKEND = env( "DJANGO_EMAIL_BACKEND", default="djcelery_email.backends.CeleryEmailBackend")添加邮箱配置项:
EMAIL_HOST = env('DJANGO_EMAIL_HOST', default="smtp.qq.com")EMAIL_USE_SSL = env('DJANGO_EMAIL_USE_SSL', default=True)EMAIL_PORT = env('DJANGO_EMAIL_PORT', default=465)EMAIL_HOST_USER = env('DJANGO_EMAIL_HOST_USER')EMAIL_HOST_PASSWORD = env('DJANGO_EMAIL_HOST_PASSWORD')DEFAULT_FROM_EMAIL = env('DJANGO_DEFAULT_FROM_EMAIL')编写celery文件
在项目配置的目录下新增celery_app.py文件
import osfrom celery import Celery# set the default Django settings module for the 'celery' program.os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")app = Celery("你的项目名")# Using a string here means the worker doesn't have to serialize# the configuration object to child processes.# - namespace='CELERY' means all celery-related configuration keys# should have a `CELERY_` prefix.app.config_from_object("django.conf:settings", namespace="CELERY")# Load task modules from all registered Django app configs.app.autodiscover_tasks()在某app目录下新增tasks.py文件:
from django.conf import settingsfrom django.core.mail import send_mailfrom config import celery_app# 打上装饰器celery就会自动搜索到添加到任务中@celery_app.task()def send_activity_verify_email(): """异步发送新订单邮件""" title = '[学时通] 您有一个新活动等待审核' message = '您有一个新活动等待审核,请进入学时通管理系统查看' send_mail(title, message=message, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=settings.EMAIL_RECIPIENT_LIST)启动celery
来到项目目录下:
执行以下命令
其中config为我的项目配置目录,config.celery_app指我项目配置目录下的celery_app文件
$ /usr/local/python3/bin/celery -A config.celery_app worker -l info当点击发送邮件时,celery会把任务添加到redis 1库中,邮箱发送完毕后,会在redis 2库中显示结果。
以上这篇Django集成celery发送异步邮件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
起步在《分布式任务队列Celery使用说明》中介绍了在Python中使用Celery来实验异步任务和定时任务功能。本文介绍如何在Django中使用Celery。
本文实例讲述了C#实现异步发送邮件的方法。分享给大家供大家参考。具体如下:下面的代码可以实现异步发送邮件,等邮件发送出去后会自动调用回调函数,这样在发送邮件时就
上一篇已经介绍了celery的基本知识,本篇以一个小项目为例,详细说明django框架如何集成celery进行开发。本系列文章的开发环境:window7+pyt
如下所示:pip3installdjango-celery-resultsINSTALLED_APPS=(...,'django_celery_results'
Celery简介除了redis,还可以使用另外一个神器---Celery。Celery是一个异步任务的调度工具。Celery是DistributedTaskQu