0Pricing
Flask Academy · レッスン

コンテキストローカルとスレッドセーフティ

プロキシがリクエスト間で正しく動作する仕組みを学びます

「コンテキストローカルとスレッドセーフティ」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「コンテキストローカルとスレッドセーフティ」レッスンは無料ですか?

はい。「コンテキストローカルとスレッドセーフティ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「コンテキストローカルとスレッドセーフティ」で何を学びますか?

プロキシがリクエスト間で正しく動作する仕組みを学びます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「コンテキストローカルとスレッドセーフティ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. アプリコンテキストとリクエストコンテキスト
  2. current_appとgオブジェクト
  3. コンテキストローカルとスレッドセーフティ
  4. スクリプトとシェルでコンテキストをプッシュする
← Flask Academyに戻る