-
安裝
pip install django==1.11.7 pip install celery
-
專案目錄
AXF ├── alipay_config │ ├── alipay_rsa_public_key.pem │ └── app_rsa_private_key.pem ├── App │ ├── admin.py │ ├── apps.py │ ├── __init__.py │ ├── migrations │ │ ├── __init__.py │ │ └── __pycache__ │ ├── models.py │ ├── __pycache__ │ ├── tests.py │ ├── urls.py │ ├── views_contstant.py 封裝傳送郵件函式 │ └── views.py ├── AXF │ ├── __init__.py │ ├── __pycache__ │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── task.py celery非同步任務 ├── manage.py ├── middleware ├── static
-
settings配置
#email EMAIL_BACKEND = `django.core.mail.backends.smtp.EmailBackend` EMAIL_USE_TLS = False EMAIL_USE_SSL = True EMAIL_HOST = `smtp.163.com` EMAIL_PORT = 465 EMAIL_HOST_USER = `sajinde@163.com` EMAIL_HOST_PASSWORD = `**********` DEFAULT_FROM_EMAIL = `sam <sajinde@163.com>`
-
celery模組
#在task檔案中 from __future__ import absolute_import, unicode_literals import os from celery import Celery # set the default Django settings module for the `celery` program. # AXF.settings:專案下的settings檔案 os.environ.setdefault(`DJANGO_SETTINGS_MODULE`, `AXF.settings`) broker = `redis://127.0.0.1:6379/6` # 將要進行的任務 backend = `redis://127.0.0.1:6379/7` # 返回的結果 app = Celery(`App`, broker=broker, backend=backend) # app = Celery(`App`) # 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() #自動搜尋所有的任務 `debug開啟` # @app.task(bind=True) # def debug_task(self): # print(`Request: {0!r}`.format(self.request))
-
App下的init檔案
from __future__ import absolute_import, unicode_literals from task import app as celery_app __all__ = [`celery_app`]
-
傳送郵件
from celery import shared_task @shared_task #可多次重用,@shared_task裝飾可以讓你無需任何具體的應用程式例項建立任務 def send_register_mail(username, email, token): # 傳送郵件 print(`------------------------------------`) sleep(10) print(`------------------------------------`) title = `啟用` msg = ` ` data = { `username`: username, `url`: `http://127.0.0.1:8000/app/active/?token=` + token } temp = loader.get_template(`user/active.html`) html = temp.render(data) receiver = [ email, ] send_mail(title, msg, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=receiver, html_message=html) print(`send mail`)
-
傳送郵件呼叫
send_register_mail.delay(username, email, token)
-
開啟任務佇列
celery -A task worker -l info 或者 celery -A task worker --loglevel=info
Django+Celery非同步傳送郵件
相關文章
- 郵件傳送
- 傳送郵件
- SQL Server 非sysadmin賬號傳送郵件SQLServer
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- Laravel 傳送郵件Laravel
- PHP傳送郵件PHP
- Django——郵件傳送Django
- java郵件傳送Java
- Laravel傳送郵件Laravel
- gmail傳送郵件AI
- Oracle郵件傳送Oracle
- java傳送郵件Java
- Powershell郵件傳送
- thinkphp 郵件傳送PHP
- centos 傳送郵件CentOS
- phpcms傳送郵件PHP
- 郵件的傳送
- Laravel 使用 rabbitmq 非同步傳送郵件小案例.LaravelMQ非同步
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- golang傳送郵件(qq郵箱)Golang
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- 定時傳送郵件
- thinkjs 傳送郵件JS
- 使用phpmailer傳送郵件PHPAI
- Ubuntu的郵件傳送Ubuntu
- python傳送郵件Python
- nodejs 傳送郵件NodeJS
- 傳送郵件那些事
- javamail郵件傳送例子JavaAI
- 使用JavaMail傳送郵件JavaAI
- 使用nodemailer傳送郵件AI
- javamail傳送郵件(轉)JavaAI
- 使用python傳送郵件和接收郵件Python
- 如何傳送電子郵件到別人郵箱?電子郵件傳送的方法
- 使用 smtplib 傳送郵件
- Laravel 傳送郵件報錯Laravel
- Android 快速傳送郵件Android