-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi_di_yield.py
More file actions
40 lines (27 loc) · 796 Bytes
/
fastapi_di_yield.py
File metadata and controls
40 lines (27 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
from fastapi import Depends, FastAPI
async def dependency_a():
session_a = random.randint(1, 100)
try:
yield session_a
finally:
del session_a
async def dependency_b(dep_a=Depends(dependency_a)):
session_b_with_a = f"{dep_a}_{random.randint(1, 100)}"
try:
yield session_b_with_a
finally:
del session_b_with_a
async def dependency_c(dep_b=Depends(dependency_b)):
session_c_with_b = f"{dep_b}_{random.randint(1, 100)}"
try:
yield session_c_with_b
finally:
del session_c_with_b
app = FastAPI()
@app.get("/items/")
async def read_items(c_value: str = Depends(dependency_c)):
return [
{"c_value": c_value},
]
# PYTHONPATH=module_fastapi uvicorn fastapi_di_yield:app --reload