Flask-mail 发邮件慢(即使异步)
Flask-mail 发邮件慢(即使异步)
一开始,按照狗书上的代码异步发邮件,但是发现原本响应只需要150ms的页面加了邮件发送就变成了5s响应(这怕不是假异步)
狗书的异步发邮件代码:
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(to, subject, template, **kwargs):
app = current_app._get_current_object()
msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
print datetime.datetime.now().second
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
print datetime.datetime.now().second
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()
print datetime.datetime.now().second
return thr
之后我在每一个可能延时的位置加了一句print datetime.datetime.now().second,最后发现,真正拖时间的地方居然是在Message初始化的时候!
msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
于是自己优化了一下发邮件的代码,把Message初始化的部分也放进了异步线程中。
优化后,速度明显提升:)
我的优化代码:
def send_async_email(app, to, subject, template_done_html, template_done_txt):
with app.app_context():
msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
msg.body = template_done_txt
msg.html = template_done_html
mail.send(msg)
def send_email(to, subject, template, **kwargs):
app = current_app._get_current_object()
template_done_html = render_template(template + '.html', **kwargs)
template_done_txt = render_template(template + '.txt', **kwargs)
thr = Thread(target=send_async_email, args=[app, to, subject, template_done_html, template_done_txt])
thr.start()
return thr
Flask-mail 发邮件慢(即使异步)的更多相关文章
- shell中mail发邮件的问题
今天为了监控一下脚本,按照网上说的利用mail 发邮件,mail -s "error预警2" peien@1221.qq.com<'邮件内容',发现出现cc,不知道啥问题,也 ...
- 关于java mail 发邮件的问题总结(转)
今天项目中有需要用到java mail发送邮件的功能,在网上找到相关代码,代码如下: import java.io.IOException; import java.util.Properties; ...
- 关于使用Java Mail发邮件的问题
今天做东西的时候突然遇到需要发邮件的问题,然后就使用SMTP协议进行邮件的发送.用了一个工具类简化邮件发送的功能, 在这次试验中,我使用了自己的QQ邮箱进行发送邮件的发送者. public class ...
- linux lnmp下无法使用mail发邮件的两种解决方法
在配置了lnmp环境后,出现了mail函数不能发送邮件的问题,其实有两种方法,一是使用sendmail组件,而是使用postfix. 方法一,使用sendmail组件来发邮件 1.安装 sendma ...
- spring boot(16)-mail发邮件
上一篇讲了如何处理异常,并且异常最终会写入日志.但是日志是写在服务器上的,我们无法及时知道.如果能够将异常发送到邮箱,我们可以在第一时间发现这个异常.当然,除此以外,还可以用来给用户发验证码以及各种离 ...
- 发现用System.Net.Mail发邮件(代码附后),附件稍微大一点就会造成程序假死. 有没有什么简单的解决办法呢? 多谢!!
附件大,上传,发送一定会慢.程序卡,应该是主线程正在发送,邮件造成的.创建其他线程在后台去发.这样就不影响主线程做其他工作了 using System; using System.Collecti ...
- 利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)
Spring提供了发送电子邮件的支持,可以发送普通邮件.带附件邮件.HTML格式邮件,甚至还可以使用Velocity模板定制化邮件内容. 一.引入相关的库 1 2 3 4 5 6 7 8 9 10 1 ...
- mail发邮件报错 "send-mail: fatal: parameter inet_interfaces: no local interface found for ::1"
发送邮件: [root@itfswelog123]# echo '测试邮件标题' | mail -s "数据库挂啦.挂啦.起床啦 " xx@163.com 出现异常: [r ...
- linux mail 发邮件
system('echo "'.$xmlHeader.$xmlBody.$xmlFooter.'" | mail -s "百度新闻源生成成功,地址=>http:// ...
随机推荐
- python 中numpy的var,std及cov
var:表示方差, 即各项-均值的平方求和后再除以N , std:表示标准差,是var的平方根. cov:协方差 ,与var类似,但是除以(N-1)
- spring常用接口 InitializingBean的作用
工作中遇到spring接口中的InitializingBean接口.浅浅的解说一下. --------------------------------------------------------- ...
- ios中改变UIImagePickerController页面的button的文字为中文
可以在工程中直接 project-->info-->Localization native development region 赋值为 zh_CN
- 23 DesignPatterns学习笔记:C++语言实现 --- 2.1 Bridge
23 DesignPatterns学习笔记:C++语言实现 --- 2.1 Bridge 2016-07-22 (www.cnblogs.com/icmzn) 模式理解
- VS2017中对C++的单元测试
安装Visual Studio 2017 由于平时都是用codeblock,因此电脑中没有装VS系列的IDE,就从安装开始吧 最开始安装的时候没有注意什么都没选,安装完了以后根本没有c++的编译器和各 ...
- Receiving Transaction Processor Conundrum
what would we do if we are faced with a situation to execute a receiving transaction in oracle ebusi ...
- Android阻止AlertDialog关闭
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("测试" ...
- 开源应用框架BitAdminCore:更新日志20180605
索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...
- WCF实现进程间管道通信Demo
一.代码结构: 二.数据实体类: using System; using System.Collections.Generic; using System.Linq; using System.Run ...
- 1-初步了解C#-语言基础
本篇博客对应视频讲解 前言 终于开始讲语言了,我选择讲C#.为什么呢?因为C#是很好的入门语言,简洁.全面,面向对象类型安全,开发体验好,上手容易.而其他的语言也已经有人讲了很多了,C#相对来说要少一 ...