المحليات السياقية وأمان الخيوط
افهم كيف تحافظ الوكلاء على صحتها عبر الطلبات.
المحليات السياقية وأمان الخيوط درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 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. 🎉
الأسئلة الشائعة
هل درس «المحليات السياقية وأمان الخيوط» مجاني؟
نعم — نص درس «المحليات السياقية وأمان الخيوط» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «المحليات السياقية وأمان الخيوط»؟
افهم كيف تحافظ الوكلاء على صحتها عبر الطلبات. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «المحليات السياقية وأمان الخيوط»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- سياق التطبيق مقابل سياق الطلب
- current_app والكائن g
- المحليات السياقية وأمان الخيوط
- دفع سياق في البرامج النصية والأصداف