首页 > 解决方案 > 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 那样快速加载,电子邮件也很慢。我无法快速发送邮件。请分享如何解决这个问题?

标签: templatesflaskoptimizationrenderflask-mail

解决方案


该网站可能加载缓慢,因为您正在发送电子邮件(建立连接等)。这可能会花费大量时间。

考虑使用带有flask-rq2的作业服务(例如redis)在后台发送电子邮件通知。这将允许您启动/安排在后台发送电子邮件并更快地提供响应的作业。

对烧瓶作业队列的一个很好的介绍是烧瓶大型教程-后台作业


推荐阅读