templates - Flask 邮件注册页面模板需要 10 秒才能加载
问题描述
视频链接:https : //www.loom.com/share/d246f658c9ad442381e4c3f21c155d33 代码 app.py
from flask_mail import Mail, Message
app.config['MAIL_SERVER']='smtp.office365.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USERNAME'] = 'realdomain'
app.config['MAIL_PASSWORD'] = 'sample2'
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
app.config['MAIL_DEFAULT_SENDER '] = 'name <email>'
mail = Mail(app)
from modals import *
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
print("Welcome")
def send_mail(email):
msg = Message(subject="Scrapepilot signup activation",
recipients=[email])
msg.body="Thank you for signing up on Scrapepilot. Please click the link below to activate your account."
msg.html=render_template('activate.html', email=email)
mail.send(msg)
蓝图首页代码:
@home.route('/signup', defaults={'page': 'index'}, methods=['GET', 'POST'])
def signup(page):
try:
if request.method == 'POST':
from app import db
from modals import User # User.query.filter_by(email='mahrukh.ayub1@gmail.com').first()#
user = User(username=request.form['name'], email=request.form['email'], password=request.form['password'],
active="0", role="1")
db.session.add(user)
db.session.commit()
from app import send_mail
send_mail(request.form['email'])
return render_template('signup.html', data="key")
return render_template('signup.html')
except TemplateNotFound:
abort(404)
请查看上面的视频并阅读上述代码。我按照教程编写它并且我已经托管在 IIS 服务器上。网站运行良好,但注册页面不像 php 那样快速加载,电子邮件也很慢。我无法快速发送邮件。请分享如何解决这个问题?
解决方案
该网站可能加载缓慢,因为您正在发送电子邮件(建立连接等)。这可能会花费大量时间。
考虑使用带有flask-rq2的作业服务(例如redis)在后台发送电子邮件通知。这将允许您启动/安排在后台发送电子邮件并更快地提供响应的作业。
对烧瓶作业队列的一个很好的介绍是烧瓶大型教程-后台作业。
推荐阅读
- javascript - Nodejs, Javascript中的日期格式识别
- android - 当您使用谷歌地图和 Kotlin Android 点击不同的标记时如何更改 intent.putExtra
- redux - 使用 redux-toolkit 切换步骤更新存储状态而不是视图
- python - Scrapy 使用一个进程(AWS Lambda)运行 2 个蜘蛛并输出到 2 个不同的文件
- mysql - 通过具有不同类型的一列(另一列),同一张表计算余额
- azure-cli - az network lb rule create:错误:参数--resource-group/-g:需要一个参数
- ios - 具有相同代码的适用于 iOS 和 macOS Catalyst 应用程序的 SceneKit 上的不同对象颜色
- c++ - 根据模板的基本类型创建不同的模板类
- python-3.x - 在动态网页中使用 Python 抓取按钮下载 csv
- javascript - Firebase 数据被多次返回