python
30 lines · 5 steps
Timing requests with Flask hooks
Two request lifecycle hooks measure how long each request takes and attach the result as response headers.
Explained by
highlit
1import time
2
3from flask import Flask, g, request
4
5app = Flask(__name__)
6
7
8@app.before_request
9def start_timer():
10 g.request_start = time.perf_counter()
11
12
13@app.after_request
14def add_timing_header(response):
15 start = g.get("request_start")
16 if start is None:
17 return response
18
19 elapsed_ms = (time.perf_counter() - start) * 1000
20 response.headers["X-Response-Time"] = f"{elapsed_ms:.2f}ms"
21 response.headers["Server-Timing"] = f"app;dur={elapsed_ms:.2f}"
22
23 app.logger.info(
24 "%s %s -> %s in %.2fms",
25 request.method,
26 request.path,
27 response.status_code,
28 elapsed_ms,
29 )
30 return response
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Flask's request-scoped `g` object is the natural place to stash per-request state between hooks.
- 2`before_request` and `after_request` let you wrap logic around every request without touching individual view functions.
- 3Emitting `Server-Timing` headers surfaces backend latency directly in browser devtools.
Related explainers
python
from flask import Blueprint, jsonify, request, abort v1 = Blueprint("users_v1", __name__) v2 = Blueprint("users_v2", __name__)
Versioning a Flask API with Blueprints
api versioning
blueprints
rest
Intermediate
7 steps
python
import time import threading from enum import Enum from functools import wraps
Building a circuit breaker in Python
circuit-breaker
resilience
decorators
Advanced
7 steps
php
<?php namespace App\Http\Middleware;
Idempotent requests with Laravel middleware
idempotency
middleware
caching
Advanced
8 steps
python
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector from django.core.cache import cache from django.db.models import F from django.http import JsonResponse
Ranked full-text search in Django
full-text-search
debouncing
caching
Advanced
9 steps
python
import os from datetime import timedelta
Class-based config in a Flask app factory
configuration
app-factory
inheritance
Intermediate
7 steps
go
package middleware import ( "net/http"
A maintenance-mode gate in Gin
middleware
atomic-flag
concurrency
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/timing-requests-with-flask-hooks-explained-python-7a07/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.