Skip to content

Archive

Asyncio

1 articles
Python 02 Sep 2026 4 min read

Request-Scoped State in Python with contextvars

Applications often need small pieces of context to follow a request through several layers: a request ID, tenant identifier, locale, or tracing field. Passing every value through every function is explicit, but can become noisy when the value is cross-cutting rather than part of the function’s business input. Python’s contextvars module provides context-local state designed to work with asynchronous code. Why a normal global is unsafe A module-level variable is shared by all concurrent requests: