You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define a startup and shutdown logic using the lifespan parameter of the FastAPI app, and a "context manager" (I'll show you what that is in a second).
Let's start with an example and then see it in detail.
We create an async function lifespan() with yield like this:
fromcontextlibimportasynccontextmanagerfromfastapiimportFastAPIdeffake_answer_to_everything_ml_model(x: float):
returnx*42ml_models= {}
@asynccontextmanagerasyncdeflifespan(app: FastAPI):
# Load the ML modelml_models["answer_to_everything"] =fake_answer_to_everything_ml_modelyield# Clean up the ML models and release the resourcesml_models.clear()
app=FastAPI(lifespan=lifespan)
@app.get("/predict")asyncdefpredict(x: float):
result=ml_models["answer_to_everything"](x)
return {"result": result}
Define a startup and shutdown logic using the lifespan parameter of the FastAPI app, and a "context manager" (I'll show you what that is in a second).
Let's start with an example and then see it in detail.
We create an async function lifespan() with yield like this:
Source: https://fastapi.tiangolo.com/advanced/events/
The text was updated successfully, but these errors were encountered: