@ -17,6 +17,8 @@ To do this, use `yield` instead of `return`, and write the extra steps after.
In fact, FastAPI uses those two decorators internally.
In fact, FastAPI uses those two decorators internally.
And if you have a context manager already, you can use it as a dependency, too.
## A database dependency with `yield`
## A database dependency with `yield`
For example, you could use this to create a database session and close it after finishing.
For example, you could use this to create a database session and close it after finishing.
@ -222,32 +224,38 @@ When the `with` block finishes, it makes sure to close the file, even if there w
When you create a dependency with `yield`, **FastAPI** will internally convert it to a context manager, and combine it with some other related tools.
When you create a dependency with `yield`, **FastAPI** will internally convert it to a context manager, and combine it with some other related tools.
### Using context managers in dependencies with `yield`
## Context Managers as dependencies
You don’t have to create a Context Manager to use FastAPI dependencies.
But sometimes you might want to use a dependency both inside and outside FastAPI. For example, you might want to connect to a database in a DB migration script. You can create a Context Manager manually then:
If you are just starting with **FastAPI** you might want to skip it for now.
If you are just starting with **FastAPI** you might want to skip it for now.
In Python, you can create Context Managers by <ahref="https://docs.python.org/3/reference/datamodel.html#context-managers"class="external-link"target="_blank">creating a class with two methods: `__enter__()` and `__exit__()`</a>.
You can also create Context Managers by <ahref="https://docs.python.org/3/reference/datamodel.html#context-managers"class="external-link"target="_blank">creating a class with two methods: `__enter__()` and `__exit__()`</a>.
You can also use them inside of **FastAPI** dependencies with `yield` by using
Like the Context Managers created with `contextlib`, you can use them inside of **FastAPI** dependencies directly:
`with` or `async with` statements inside of the dependency function:
```Python hl_lines="1-9 13"
```Python hl_lines="1-9 13"
{!../../../docs_src/dependencies/tutorial010.py!}
{!../../../docs_src/dependencies/tutorial010.py!}
```
```
!!! tip
Another way to create a context manager is with:
* <ahref="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager"class="external-link"target="_blank">`@contextlib.contextmanager`</a> or
using them to decorate a function with a single `yield`.
That's what **FastAPI** uses internally for dependencies with `yield`.
But you don't have to use the decorators for FastAPI dependencies (and you shouldn't).
!!! note "Technical Details"
Internally, FastAPI calls the dependency function and then checks if the result has either an `__enter__()` or an `__aenter__()` method. If that’s the case, it will treat it as a context manager.