问题说明:
fastapi 启动之前执行代码,旧版本使用的是@app.on_event("startup")
,不过在已经不推荐使用
@app.on_event("startup")
async def startup_db():
create_all()
问题处理
通过 lifespan event配合contextlib实现。
修改后代码:
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
print("启动前执行")
create_all()
yield
print("关闭后前执行")
app = FastAPI(lifespan=lifespan)
- event DeprecationWarning deprecated on_event handlersevent deprecationwarning deprecated on_event deprecationwarning pkg_resources deprecated on_event deprecated android handler is event_handler event_handler undefined reference typeinfo deprecationwarning collections deprecationwarning 39 from desired_capabilities deprecationwarning capabilities deprecationwarning executable_path executable