Performance: Email verification pattern
This pattern affects page load speed and interaction responsiveness during user registration and verification steps.
from flask import Flask, request, jsonify from threading import Thread app = Flask(__name__) def send_verification_email_async(email): # simulate sending email import time time.sleep(5) print(f'Verification email sent to {email}') @app.route('/register', methods=['POST']) def register(): email = request.form['email'] Thread(target=send_verification_email_async, args=(email,)).start() return jsonify({'message': 'Registration complete, verification email sent asynchronously'})
from flask import Flask, request import time app = Flask(__name__) @app.route('/register', methods=['POST']) def register(): email = request.form['email'] # Simulate sending email synchronously send_verification_email(email) # blocks request until done return 'Registration complete, check your email!' def send_verification_email(email): time.sleep(5) # simulate delay print(f'Verification email sent to {email}')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous email sending | Minimal | N/A | Blocks paint until response | [X] Bad |
| Asynchronous email sending | Minimal | N/A | Immediate paint and interaction | [OK] Good |
itsdangerous library provides URLSafeTimedSerializer to create timed tokens.flask_sqlalchemy manages databases, flask_login handles user sessions, and flask_mail sends emails but does not create tokens.verify_token(token) return if the token is expired?
from itsdangerous import URLSafeTimedSerializer, SignatureExpired
serializer = URLSafeTimedSerializer('secret-key')
def verify_token(token):
try:
email = serializer.loads(token, max_age=3600)
return email
except SignatureExpired:
return None
SignatureExpired, which is caught and returns None.from itsdangerous import URLSafeTimedSerializer
serializer = URLSafeTimedSerializer('secret-key')
def generate_token(email):
token = serializer.dumps(email)
return token
# Later verification
email = serializer.loads(token, max_age=3600)
generate_token but not stored or passed outside.token is used but not defined in that scope, causing a NameError.URLSafeTimedSerializer with a secret key to generate a token that expires after 86400 seconds (24 hours).loads() with max_age=86400 to enforce expiration.URLSafeTimedSerializer, send email with token link, verify token with max_age=86400 [OK]Pass the test and this pattern moves unknown