Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post-startup event in FastAPI?

I have a FastAPI app that serves an ML model. It is deployed on Kubernetes. For best practices Kubernetes recommends implementing a liveliness endpoint in your API that it can probe to see when the application has completed startup, as well as a readiness endpoint to probe to see when the application is ready to start receiving requests.

Currently, I have implemented both the liveliness and readiness endpoints as a single endpoint, which returns a status code of 200 once the ML model has been loaded and the endpoints are available for requests.

This is ok, but ideally, I would like a liveliness endpoint to return 200 once FastAPI's startup has completed, and a readiness endpoint to return 200 once the models have been loaded (takes much longer than the application startup).

FastAPI allows for startup event triggers where I could initiate the loading of the model, but no endpoints become available until the application startup is complete, which will not be complete until the startup events are also complete.

Is there anyway to implement and "post-startup" event in FastAPI where I could initiate the loading of the model?

Here is some simple example code for what I would like to achieve:

from fastapi import FastAPI, Response
from request import PredictionRequest
import model

app = FastAPI()

@app.on_event("post-startup") # not possible
def load_model():
    model.load()

@app.get("/live")
def is_live():
    return Response(status_code=200)

@app.get("/ready")
def is_ready():
    if model.is_loaded():
        return Response(status_code=200)
    else:
        return Response(status_code=409)

@app.post('/predict')
def predict(request: PredictionRequest):
    return model.predict(request)
like image 426
KOB Avatar asked May 30 '26 10:05

KOB


1 Answers

At the moment there are only 2 events: "shutdown" and "startup"

These are a subsection of the ASGI protocol and are implemented by Starlette and available in FastAPI.

like image 91
alebychac Avatar answered Jun 01 '26 22:06

alebychac