Django集成celery发送异步邮件实例

yipeiwu_com5年前Python基础

安装依赖

pip install django-celery-beat
pip install django-celery-email
pip install celery
pip install msgpack-python
pip install msgpack

在settings文件中配置

添加app应用到installed_apps中

 "djcelery_email",
 "django_celery_beat"

修改.env文件配置:

#邮箱后端,使用celery email
DJANGO_EMAIL_BACKEND=djcelery_email.backends.CeleryEmailBackend
DJANGO_EMAIL_HOST=smtp.qq.com
DJANGO_EMAIL_HOST_USER=xxx@qq.com # 您的邮箱号
DJANGO_EMAIL_HOST_PASSWORD=你的邮箱密码
DJANGO_DEFAULT_FROM_EMAIL=默认发送的邮箱号

#CELERY
CELERY_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_url
CELERY_BROKER_URL = env("CELERY_BROKER_URL")
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_backend
CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND')
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-accept_content
CELERY_ACCEPT_CONTENT = ["json", 'msgpack']
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-task_serializer
CELERY_TASK_SERIALIZER = "msgpack"
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_serializer
CELERY_RESULT_SERIALIZER = "json"
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-time-limit
# TODO: set to whatever value is adequate in your circumstances
CELERY_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 circumstances
CELERY_TASK_SOFT_TIME_LIMIT = 60
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#beat-scheduler
CELERY_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 os
from 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 settings
from django.core.mail import send_mail

from 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发送异步邮件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Windows下为Python安装Matplotlib模块

Windows下为Python安装Matplotlib模块

  这玩意反反复复弄了一晚上,这里详细叙述下如何安装,肯定会对大家有所帮助。首先默认大家都装了Python,这个从官网下基本不会有任何难度。   (1)Setuptools的安装   为...

详解Python sys.argv使用方法

详解Python sys.argv使用方法

sys.argv是python用来获取命令行参数的,如在CMD下执行python *.py data,也就是使用python执行脚本时,需要添加的参数 按照Python教程中的做法 编写...

python多线程共享变量的使用和效率方法

python多线程可以使任务得到并发执行,但是有时候在执行多次任务的时候,变量出现“意外”。 import threading,time n=0 start=time.time()...

详解Python的Lambda函数与排序

lambda函数是一种快速定义单行的最小函数,是从 Lisp 借用来的,可以用在任何需要函数的地方。下面的例子比较了传统的函数与lambda函数的定义方式。 前几天看到了一行求1000...

PYTHON EVAL的用法及注意事项解析

前言 eval是Python的一个内置函数,这个函数的作用是,返回传入字符串的表达式的结果。想象一下变量赋值时,将等号右边的表达式写成字符串的格式,将这个字符串作为eval的参数,eva...