0Pricing
MLOps Academy · Lektion

Das Modell beim Start nur einmal laden

Verwenden Sie Lifespan-Events, damit das Laden nur einmal erfolgt.

Das Modell beim Start nur einmal laden ist eine kostenlose MLOps 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 MLOps Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MLOps Academy-Kurs umfasst insgesamt 4 Lektionen.

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

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

Häufig gestellte Fragen

Ist die Lektion „Das Modell beim Start nur einmal laden“ kostenlos?

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

Was lerne ich in „Das Modell beim Start nur einmal laden“?

Verwenden Sie Lifespan-Events, damit das Laden nur einmal erfolgt. Du übst MLOps 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 MLOps Academy zu starten?

Keine Vorkenntnisse erforderlich. MLOps 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 „Das Modell beim Start nur einmal laden“?

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 MLOps Academy-Lektion Code schreiben und ausführen?

Ja. Jede MLOps 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. Ihr erster /predict-Endpunkt
  2. Anfragen mit Pydantic validieren
  3. Das Modell beim Start nur einmal laden
  4. Eine /health-Bereitschaftsprüfung hinzufügen
← Zurück zu MLOps Academy