Coverage for docs_src / events / tutorial003_py310.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from contextlib import asynccontextmanager 1abcd

2 

3from fastapi import FastAPI 1abcd

4 

5 

6def fake_answer_to_everything_ml_model(x: float): 1abcd

7 return x * 42 1efgh

8 

9 

10ml_models = {} 1abcd

11 

12 

13@asynccontextmanager 1abcd

14async def lifespan(app: FastAPI): 1abcd

15 # Load the ML model 

16 ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model 1eifjgkhl

17 yield 1eifjgkhl

18 # Clean up the ML models and release the resources 

19 ml_models.clear() 1eifjgkhl

20 

21 

22app = FastAPI(lifespan=lifespan) 1abcd

23 

24 

25@app.get("/predict") 1abcd

26async def predict(x: float): 1abcd

27 result = ml_models["answer_to_everything"](x) 1efgh

28 return {"result": result} 1efgh