fastapi-master/docs_src/dependencies/tutorial010.py

15 lines
292 B
Python
Raw Normal View History

2024-08-24 12:41:47 +08:00
class MySuperContextManager:
def __init__(self):
self.db = DBSession()
def __enter__(self):
return self.db
def __exit__(self, exc_type, exc_value, traceback):
self.db.close()
async def get_db():
with MySuperContextManager() as db:
yield db