• django、celery异步发邮件


    django、celery异步发邮件


    • django自带的send_mail发邮件功能执行发邮件功能会因为网络的原因造成花费的时间过长,为了解决这个问题,可以用celery + redis代替
    • 安装包:
      • pip install celery
      • pip install redis
    • 在django的根目录下新建celery_tasks文件夹,在该文件夹下新建tasks.py:
      •  1 from celery import Celery
         2 from django.conf import settings
         3 from django.core.mail import send_mail
         4 
         5 app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/8')
         6 
         7 
         8 @app.task
         9 def send_register_active_email(email, username, token):
        10     subject = '测试'
        11     message = ""
        12     from_email = settings.EMAIL_FROM
        13     recipient_list = [email]
        14     html_message = '<h1>欢迎{}来到测试</h1>请点击下面链接激活<br><a href="http://http://127.0.0.1:8000/user/active?token={}">点击此处激活</a>'.format(
        15         username, token)
        16     send_mail(subject, message, from_email, recipient_list, html_message=html_message)

      • 调用该方法:
      •  1 class Register(View):
         2     def get(self, request):
         3         return render(request, 'user/register.html')
         4 
         5     def post(self, request):
         6         username = request.POST.get('username', '')
         7         password = request.POST.get('password', '')
         8         email = request.POST.get('email', '')
         9 
        10         # 校验用户名是否重复
        11         try:
        12             user = User.objects.get(username=username)
        13         except User.DoesNotExist:
        14             user = None
        15 
        16         if user:
        17             data = {'errmsg': '用户名已存在'}
        18             return JsonResponse(data)
        19         # try:
        20         #     user = User.objects.ger(email=email)
        21         # except User.DoesNotExist:
        22         #     user = None
        23         # if user:
        24         #     data = {'errmsg': '邮箱已被注册'}
        25         #     return JsonResponse(data)
        26         user = User.objects.create_user(username=username, password=password, email=email)
        27         user.is_active = 0
        28         user.save()
        29 
        30         info = {'user_id': user.id}
        31         serializer = Serializer(settings.SECRET_KEY, 3600 * 2)
        32         token = serializer.dumps(info).decode()
        33 
        34         # 发邮件
        35         send_register_active_email.delay(email, username, token)
        36         return redirect(reverse('user:login'))
      • worker端配置:
        • 将django项目文件复制到另一个文件夹
        • 修改tasks.py文件,设置初始化
        •  1 from celery import Celery
           2 from django.conf import settings
           3 from django.core.mail import send_mail
           4 import os
           5 import django
           6 
           7 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dream.settings')
           8 django.setup()
           9 
          10 app = Celery('celery_tasks.tasks', broker='redis://127.0.0.1:6379/8')
          11 
          12 
          13 @app.task
          14 def send_register_active_email(email, username, token):
          15     subject = '测试'
          16     message = ""
          17     from_email = settings.EMAIL_FROM
          18     recipient_list = [email]
          19     html_message = '<h1>欢迎{}来到测试</h1>请点击下面链接激活<br><a href="http://127.0.0.1:8000/user/active?token={}">点击此处激活</a>'.format(
          20         username, token)
          21     send_mail(subject, message, from_email, recipient_list, html_message=html_message)
        • worker端监听
        • 进入 worker端的代码的celery_tasks目录下:
          • celery -A tasks worker -l info
          • win10下可能报错:
            • pip install eventlet
            • celery -A tasks worker -l info -P eventlet
        • 当django启动调用send_mail的时候就会将发邮件的任务交给celery执行
  • 相关阅读:
    mybatis-cache model
    多线程开发(1)
    正则表达式(3) — 正则表达式库
    正则表达式(2) — 表达式规则
    我在迈瑞工作的两年总结
    正则表达式(1) — 常用的表达式
    C++系列(2) — 智能指针
    C++系列(1) — string
    路径去除前缀
    SIMD性能优化
  • 原文地址:https://www.cnblogs.com/ivy-blogs/p/10982516.html
Copyright © 2020-2023  润新知