0
0
Flaskframework~8 mins

Async email sending in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Async email sending
HIGH IMPACT
This affects the page load speed and user interaction responsiveness by offloading email sending to a background task.
Sending an email after a user submits a form
Flask
from flask import Flask, request
from flask_mail import Mail, Message
from threading import Thread

app = Flask(__name__)
mail = Mail(app)

def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)

@app.route('/submit', methods=['POST'])
def submit():
    msg = Message('Hello', recipients=[request.form['email']])
    msg.body = 'Thanks for signing up!'
    Thread(target=send_async_email, args=(app, msg)).start()  # async call
    return 'Email sending started!'
Email sending runs in a separate thread, so the HTTP response returns immediately without waiting for the email to send.
📈 Performance GainNon-blocking request, improves INP by reducing user wait time by 500ms+
Sending an email after a user submits a form
Flask
from flask import Flask, request
from flask_mail import Mail, Message

app = Flask(__name__)
mail = Mail(app)

@app.route('/submit', methods=['POST'])
def submit():
    msg = Message('Hello', recipients=[request.form['email']])
    msg.body = 'Thanks for signing up!'
    mail.send(msg)  # synchronous call
    return 'Email sent!'
The mail.send() call blocks the request until the email is sent, causing slow response times and poor user experience.
📉 Performance CostBlocks rendering and response for 500ms+ depending on email server latency
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous email sendingN/ABlocks server responseDelays first paint and interaction[X] Bad
Async email sending with threadN/ANon-blockingFaster first paint and interaction[OK] Good
Rendering Pipeline
Synchronous email sending blocks the server response, delaying the browser's ability to render or respond to user input. Async sending moves email operations off the main request thread, allowing the server to respond faster and the browser to paint sooner.
Server Response
Interaction to Next Paint (INP)
⚠️ BottleneckServer blocking during email send delays response and user interaction readiness
Core Web Vital Affected
INP
This affects the page load speed and user interaction responsiveness by offloading email sending to a background task.
Optimization Tips
1Never send emails synchronously in the main request thread to avoid blocking user interactions.
2Use background threads or task queues to handle email sending asynchronously.
3Check performance profiles to ensure no long blocking tasks delay server responses.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of sending emails asynchronously in Flask?
AIt makes the email send faster on the mail server.
BIt prevents blocking the HTTP response, improving user interaction speed.
CIt reduces the size of the email content.
DIt improves the email delivery success rate.
DevTools: Performance
How to check: Record a performance profile while submitting the form. Look for long tasks blocking the main thread during the request.
What to look for: Long blocking tasks indicate synchronous email sending; absence of blocking shows async sending.