Menu

FastAPI Lifespan Events are now supported on Vercel - Vercel
📰
0

FastAPI Lifespan Events are now supported on Vercel - Vercel

Vercel News·Ricardo Gonzalez·4 days ago
#DvQSjDUP
#vercel#fastapi#lifespan#logic#startup#photo
Reading 0:00
15s threshold

Vercel now supports lifespan events for FastAPI apps. This allows you to define logic that can execute on startup and graceful shutdown—such as managing database connections or flushing external logs.

from contextlib import asynccontextmanager

from fastapi import FastAPI

@asynccontextmanager

async def lifespan(app: FastAPI):

# Startup logic

print("Starting up...")

await startup_tasks()

yield

# Shutdown logic

await cleanup_tasks()

app = FastAPI(lifespan=lifespan)

Deploy FastAPI on Vercel or visit the FastAPI on Vercel documentation.

Read More