Context Locals and Thread Safety
How proxies stay correct across requests.
Context Locals and Thread Safety is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Magic Question
How does one global request object stay correct when many visitors hit your app at once? The answer is context locals.
They Look Global
Objects like request and g look like plain globals you import once. But they are special proxies, not ordinary shared variables.
from flask import requestProxies Point Per Context
A proxy forwards every access to the real object for the current context. Each request sees its own data through the same import.
Context Locals
Flask calls these context locals. They behave like thread locals but key off the active context, so concurrent requests never collide.
Thread Safety
Two threads handling two requests each get their own bound context. So request in one thread never shows another thread's data.
Beyond Threads
Modern Flask uses contextvars, so isolation holds even for greenlets and async tasks, not just operating system threads.
One Import, Many Values
This is why a single import at the top of your file is safe. The proxy resolves to a different real object for every request.
Watch the Background
Spawn a raw thread from a view and it has no context. Accessing request there fails unless you push a context yourself.
Pass Values, Not Proxies
Hand a background worker the actual value it needs, like a user id, instead of the request proxy, which will be gone by then.
uid = request.args.get("id")
start_job(uid)Get the Real Object
Need the underlying object behind a proxy? Use _get_current_object, but reach for it rarely and only when you truly must.
real = request._get_current_object()Why It Feels Easy
Context locals are why Flask feels simple: you import request once and trust it, while the framework quietly keeps every request isolated.
Quick Check
Let us confirm how proxies stay safe.
Recap
Flask's globals are really context-local proxies. They resolve per request, keeping threads and async tasks isolated, so one import stays safe everywhere. 🎉
Frequently asked questions
Is the “Context Locals and Thread Safety” lesson free?
Yes — the full text of “Context Locals and Thread Safety” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Context Locals and Thread Safety”?
How proxies stay correct across requests. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Context Locals and Thread Safety” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- App Context vs Request Context
- current_app and the g Object
- Context Locals and Thread Safety
- Push a Context in Scripts and Shells