FastAPI uses Depends() to inject variables either returned or yielded. Eg, FastAPI/SQL:
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
...
def create_user(db: Session = Depends(get_db)):
...
If I wanted to use that get_db()
somewhere else (outside a FastAPI route), how would I do that? I know it's Python core knowledge, but I can't seem to figure it out. My initial thought was db = yield from get_db()
, but I can't call yield from
in async functions (and don't know if it would work besides). Then I tried:
with get_db() as db:
pass
Which fails as the original get_db()
isn't wrapped as a @contextmanager
. (Note, I don't want to decorate this - I'm using get_db
as an example, I need to work with more complicated dependencies). Finally, I tried db = next(get_db())
- which works, but I don't think that's the correct solution. When/how will finally
be invoked - when my method returns? And in some other dependencies, there's post-yield code that needs to execute; would I need to call next()
again to ensure that code executes? Seems like next()
isn't the right way. Any ideas?