简体   繁体   中英

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)

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.

https://asgi.readthedocs.io/en/latest/specs/lifespan.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM