Performance: Why performance matters
Performance affects how fast the page loads and how smooth user interactions feel.
from flask import Flask, render_template import threading app = Flask(__name__) def slow_task(): import time time.sleep(5) # Runs without blocking response @app.route('/') def index(): # Start slow task in background thread threading.Thread(target=slow_task).start() return render_template('index.html')
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): # Heavy computation or slow database call inside request import time time.sleep(5) # Simulates slow processing return render_template('index.html')
| Pattern | Server Processing | Response Time | User Experience | Verdict |
|---|---|---|---|---|
| Blocking slow task in request | High CPU wait | 5+ seconds | Very poor, users wait | [X] Bad |
| Background slow task | Non-blocking | Under 1 second | Smooth, fast load | [OK] Good |
@app.route('/slow')
def slow():
import time
time.sleep(3)
return 'Done'@app.route('/data')
def data():
result = []
for i in range(1000000):
result.append(i)
return str(result)Pass the test and this pattern moves unknown