- python: 3.12 - windows: win11 <img width="300" height="100" alt="Image" src="https://github.com/user-attachments/assets/1f146b4a-9a65-482a-a198-4b1472277f60" /> <img width="300" height="100" alt="Image" src="https://github.com/user-attachments/assets/55e65eb1-8530-4876-ae4c-ae4695c227a6" /> ## pyproject.toml ```toml [project] name = "fastapi-granian-demo" version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" dependencies = [ "fastapi>=0.116.1", "granian[pname,reload,rloop]>=2.4.2", ] ``` ## main.py ```py from contextlib import asynccontextmanager from fastapi import FastAPI from granian.constants import Interfaces from granian.server import Server @asynccontextmanager async def lifespan(app: FastAPI): print("Service startup...") yield print("Service shutdown...") # 资源清理逻辑 print("Resources released, service gracefully shut down") app = FastAPI(title="FastAPIGranianDemo", lifespan=lifespan) @app.get("/") async def root(): return {"message": "Hello World"} def main(): server = Server( target="main:app", address="127.0.0.1", port=8000, interface=Interfaces.ASGI ) # server.stop() # server.reload() # 启动服务 server.serve() if __name__ == "__main__": main() ```