简体   繁体   English

FastAPI 中的启动后事件?

[英]Post-startup event in FastAPI?

I have a FastAPI app that serves an ML model.我有一个提供 ML model 的 FastAPI 应用程序。 It is deployed on Kubernetes.它部署在 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.对于最佳实践 Kubernetes 建议在您的 API 中实现一个活泼端点,它可以探测以查看应用程序何时完成启动,以及一个就绪端点以探测以查看应用程序何时准备好开始接收请求。

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.目前,我已将 liveliness 和 readiness 端点实现为单个端点,一旦加载了 ML model 并且端点可用于请求,它将返回状态代码200

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 的启动完成后返回200的活跃度端点,并在加载模型后返回200的就绪端点(比应用程序启动需要更长的时间)。

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. FastAPI 允许启动事件触发器,我可以在其中启动 model 的加载,但在应用程序启动完成之前没有端点可用,在启动事件也完成之前不会完成。

Is there anyway to implement and "post-startup" event in FastAPI where I could initiate the loading of the model?无论如何要在 FastAPI 中实现和“启动后”事件,我可以在其中启动 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"目前只有 2 个事件:“关闭”和“启动”

These are a subsection of the ASGI protocol and are implemented by Starlette and available in FastAPI.这些是 ASGI 协议的一个子部分,由 Starlette 实现并在 FastAPI 中可用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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