0Pricing
Flask Academy · Lektion

Context Locals und Thread-Sicherheit

Wie Proxys über mehrere Requests hinweg korrekt bleiben.

Context Locals und Thread-Sicherheit ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 request

Proxies 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. 🎉

Häufig gestellte Fragen

Ist die Lektion „Context Locals und Thread-Sicherheit“ kostenlos?

Ja — der vollständige Text von „Context Locals und Thread-Sicherheit“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Context Locals und Thread-Sicherheit“?

Wie Proxys über mehrere Requests hinweg korrekt bleiben. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Flask Academy zu starten?

Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Context Locals und Thread-Sicherheit“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?

Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. App Context und Request Context
  2. current_app und das g-Objekt
  3. Context Locals und Thread-Sicherheit
  4. Einen Context in Skripten und Shells pushen
← Zurück zu Flask Academy