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:// ...
随机推荐
- redhat6.7在线安装postgresql9
原文:http://wandejun1012.iteye.com/blog/2015777 1.安装postgresql9.0 yum 仓库 rpm -i http://yum.postgresql. ...
- HDU 6118 度度熊的交易计划 (最小费用流)
度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- UVa 1151 Buy or Build (最小生成树+二进制法暴力求解)
题意:给定n个点,你的任务是让它们都连通.你可以新建一些边,费用等于两点距离的平方(当然越小越好),另外还有几种“套餐”,可以购买,你购买的话,那么有些边就可以连接起来, 每个“套餐”,也是要花费的, ...
- 跳转AppStore 评分
-(void)goToAppStore { NSString *str = [NSString stringWithFormat: @"itms-apps://ax.itunes.apple ...
- Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)
摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...
- ZSTU4266 回文 2017-03-22 14:25 55人阅读 评论(0) 收藏
4266: 回文 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1636 Solved: 504 Description 小王想知道一个字符串是否为 ...
- EBS R12 Vision Profile default value - IRC: Geocode Host
Profile Option Name Site Application Responsibility Server Server Org User IRC: Geocode Host http:// ...
- ubuntu 安装 hubicfuse
如果你没有gcc,请先安装gcc: 1: apt-get install build-essential 1. 从github上clone源码: https://github.com/TurboGit ...
- 用VC实现特定编辑框上对回车键响应
一.引言 在通常的以CEditView为基类的单文档/多文档视图程序中,可以很好的响应键盘输入的回车键,只需比较最近两次的输入的字符,看看最新输入的字符是否内码是13(0x0d,回车键的内码)即可识别 ...
- java环境和Tomcat环境
这些变量名是一样的,变量的值需要自己根据自己的安装位置来确定 JAVA_HOME C:\Program Files\Java\jdk1.8.0_151 CATALINA_HOME(这个可能不需要) D ...