0Pricing
MLOps Academy · درس

تحميل النموذج مرة واحدة عند بدء التشغيل

استخدم أحداث lifespan لضمان حدوث التحميل مرة واحدة فقط.

تحميل النموذج مرة واحدة عند بدء التشغيل درس مجاني في MLOps Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MLOps Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MLOps Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Slow-Endpoint Trap

If you call joblib.load inside /predict, the model reloads on every request. That is slow and wasteful for large models. 🐢

Load It Just Once

The fix: load the model a single time when the service starts, keep it in memory, and reuse it for every prediction.

The Lifespan Hook

FastAPI gives you a lifespan function to run setup before serving and cleanup after. It is the right home for model loading.

Write an Async Context Manager

You decorate an async function with asynccontextmanager. Code before yield runs at startup; code after runs at shutdown.

from contextlib import asynccontextmanager

Stash the Model in State

Load the model at startup and save it on app.state, a shared place every request handler can reach later.

@asynccontextmanager
async def lifespan(app):
    app.state.model = joblib.load("model.joblib")
    yield

Wire It to the App

Pass your lifespan function when you create the app. Now FastAPI runs your loading code once as the server boots.

app = FastAPI(lifespan=lifespan)

Read It in the Route

Inside /predict you grab the already-loaded model from app.state. No disk read, just a fast in-memory lookup.

@app.post("/predict")
def predict(req: Request):
    model = req.app.state.model

Clean Up at Shutdown

Anything after the yield runs when the app stops, perfect for closing files or freeing GPU memory the model held.

    yield
    app.state.model = None

Faster First Request

Because loading finished at startup, even the very first user gets a fast response, not a cold-load penalty. ⚡

Watch the Worker Count

Each uvicorn worker is its own process with its own copy of the model. More workers means more memory, so size them deliberately.

Why It Matters

Loading once is a core serving optimization: it cuts latency, lowers disk I/O, and keeps memory predictable under real traffic. 📈

Quick Check

You want your model loaded exactly once when the service boots. Where should that happen?

Recap

You moved loading into a lifespan hook, stored the model on app.state, and read it per request. One load, fast responses, clean shutdown. 🙌

الأسئلة الشائعة

هل درس «تحميل النموذج مرة واحدة عند بدء التشغيل» مجاني؟

نعم — نص درس «تحميل النموذج مرة واحدة عند بدء التشغيل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MLOps Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MLOps Academy 4 دروس في المجموع.

ماذا ستتعلم في «تحميل النموذج مرة واحدة عند بدء التشغيل»؟

استخدم أحداث lifespan لضمان حدوث التحميل مرة واحدة فقط. تتمرن على MLOps Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ MLOps Academy؟

لا تُشترط خبرة سابقة. MLOps Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «تحميل النموذج مرة واحدة عند بدء التشغيل»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس MLOps Academy هذا؟

نعم. كل درس في MLOps Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. نقطة النهاية /predict الأولى
  2. التحقق من الطلبات باستخدام Pydantic
  3. تحميل النموذج مرة واحدة عند بدء التشغيل
  4. إضافة فحص الجاهزية /health
← العودة إلى MLOps Academy