From ce4002e01a114bebbe5ff2ae4d9f90f197f69ab6 Mon Sep 17 00:00:00 2001 From: Dushyant Bhardwaj Date: Sat, 12 Oct 2024 13:25:42 +0530 Subject: [PATCH] updated the fastAPI backend with latest specifications of lifespans --- backend/main.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/backend/main.py b/backend/main.py index 6ea4543..e2384a4 100644 --- a/backend/main.py +++ b/backend/main.py @@ -5,6 +5,7 @@ from time import sleep from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware +from contextlib import asynccontextmanager from app.database.faces import cleanup_face_embeddings, create_faces_table from app.database.images import create_image_id_mapping_table, create_images_table @@ -17,11 +18,8 @@ from app.routes.facetagging import router as tagging_router -app = FastAPI() - - -@app.on_event("startup") -async def startup_event(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_YOLO_mappings() create_faces_table() create_image_id_mapping_table() @@ -29,14 +27,12 @@ async def startup_event(): create_albums_table() cleanup_face_embeddings() init_face_cluster() - - -@app.on_event("shutdown") -async def shutdown_event(): + yield face_cluster = get_face_cluster() if face_cluster: face_cluster.save_to_db() +app = FastAPI(lifespan=lifespan) # Add CORS middleware app.add_middleware(