lifepsan not running while running the fastapi, only the old version of lifespan is working

214 Views Asked by At
@asynccontextmanager
async def lifespan(app: FastAPI):
    print("startup")
    await news_db_client.connect()
    yield
    await news_db_client.disconnect()


app = FastAPI(lifespan=lifespan)

this part of code doesnt get executed, but when tyring the old way of lifespan :

@app.on_event("startup")
async def startup_db_client():
    print("startup")
    await news_db_client.connect()


@app.on_event("shutdown")
async def startup_db_client():
    await news_db_client.disconnect()

it works.

I tried to run the code and just get print of the startup to see if it works.

0

There are 0 best solutions below