django + celery + redis 异步发送QQ邮箱
django一般的代码逻辑都需要一步一步执行,这样大大的占用了I/O资源,用户也得不到完美的体验,django-celery可以灵活的解决这一问题,并且还支持定时任务。
-
创建一个django项目,流程咱就不说了
-
在setting.py里面配置邮箱服务
EMAIL_HOST = 'smtp.qq.com' EMAIL_PORT = 25 #发件箱的smtp服务器端口 EMAIL_HOST_USER = '' # 你的 QQ 账号 EMAIL_HOST_PASSWORD = ''#在邮箱中设置的客户端授权密码 EMAIL_USE_TLS = True # 这里必须是 True,否则发送不成功 EMAIL_FROM = '' # 你的 QQ 账号 DEFAULT_FROM_EMAIL = ''# 收件人看到的发件人
-
编写view.py,体验普通发送邮箱
def Send_email(request): # 发送邮件 email_title = '我是标题' email_body = '我是内容' email = '@qq.com' # 需要发送的qq邮箱 EMAIL_FROM = '@qq.com' # 我的qq邮箱 send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: res = '发送成功' else: res = '发送失败' return HttpResponse(res)
-
上面没有毛病的话,接下来就开始进入正题操作celery了
-
pip下载:pip install celery
-
错误提示
1. 错误提示:celery:Unrecoverable error: AttributeError("'unicode' object has no attribute 'iteritems') 原因:redis版本太高 解决办法:1.pip uninstall redis 2.pip install redis==2.10.6 2. 错误提示:Received unregistered task of type 'polls.tasks.add'. 解决办法:在@app.task()加入name-->@app.task(name='polls.tasks.add') 3. 错误提示:DisabledBackend' object has no attribute '_get_task_meta_for 解决办法:检查app = Celery('tasks',backend='redis://localhost:6379/0', broker='redis://localhost:6379/0')是否有backend,broker 如果app少参数,添加完之后重启:celery -A tasks worker --loglevel=info 4. 错误提示:django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 解决办法: 在tasks.py文件里面添加: import django import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', '项目name.settings') django.setup() #重启
-
在django app下面创建一个tasks.py并编辑内容
from celery import Celery from django.core.mail import send_mail #在任务处理这加以下代码 import django import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery.settings') django.setup() #创建Celery类的实例对象 app = Celery('tasks',backend='redis://localhost:6379/0', broker='redis://localhost:6379/0') #配置好celery的backend和broker# backend='redis://localhost:6379/0', CELERY_ACCEPT_CONTENT = ['pickle'] @app.task(name='polls.tasks.send_email') #普通函数装饰为 celery task def send_email(): email_title = '我是标题' email_body = '我是内容' email = '@qq.com' # 需要发送的qq邮箱 EMAIL_FROM = '@qq.com' # 我的qq邮箱 send_status = send_mail(email_title, email_body, EMAIL_FROM, [email]) if send_status: res = '发送成功' else: res = '发送失败' return res
-
def Send_email(request): result =send_email.delay() while not result.ready(): time.sleep(1) print('task done: {0}'.format(result.get())) return HttpResponse('OK')
-
运行,本人亲测成功
-
关于Celery在网上找的资料网址:
Celery 官方文档英文版:http://docs.celeryproject.org/en/latest/index.html
Celery 官方文档中文版:http://docs.jinkan.org/docs/celery/
celery配置:http://docs.jinkan.org/docs/celery/configuration.html#configuration
参考:http://www.cnblogs.com/landpack/p/5564768.html http://blog.youkuaiyun.com/happyAnger6/article/details/51408266
http://www.cnblogs.com/forward-wang/p/5970806.html
分布式队列神器 Celery:https://segmentfault.com/a/1190000008022050
celery最佳实践:https://my.oschina.net/siddontang/blog/284107
Celery 分布式任务队列快速入门:http://www.cnblogs.com/alex3714/p/6351797.html
异步任务神器 Celery 快速入门教程:https://blog.youkuaiyun.com/chenqiuge1984/article/details/80127446
定时任务管理之python篇celery使用:http://student-lp.iteye.com/blog/2093397
异步任务神器 Celery:http://python.jobbole.com/87086/
celery任务调度框架实践:https://blog.youkuaiyun.com/qq_28921653/article/details/79555212
Celery-4.1 用户指南: Monitoring and Management Guide:https://blog.youkuaiyun.com/libing_thinking/article/details/78592801
Celery安装及使用:https://blog.youkuaiyun.com/u012325060/article/details/79292243
Celery学习笔记(一):https://blog.youkuaiyun.com/sdulsj/article/details/73741350