# ๐ โฎ๏ธ ๐พ
FastAPI ๐โ๐ฆบ ๐ ๐ โ ๐ โฎ๏ธ ๐.
๐, โ๏ธ `yield` โฉ๏ธ `return`, & โ โ ๐ โฎ๏ธ.
!!! tip
โ ๐ญ โ๏ธ `yield` 1๏ธโฃ ๐ ๐ฐ.
!!! note "๐ก โน"
๐ ๐ข ๐ โ โ๏ธ โฎ๏ธ:
* `@contextlib.contextmanager` โ๏ธ
* `@contextlib.asynccontextmanager`
๐ โ โ๏ธ **FastAPI** ๐.
๐, FastAPI โ๏ธ ๐ 2๏ธโฃ ๐จโ๐จ ๐.
## ๐ฝ ๐ โฎ๏ธ `yield`
๐ผ, ๐ ๐ช โ๏ธ ๐ โ ๐ฝ ๐ & ๐ โซ๏ธ โฎ๏ธ ๐.
๐ด ๐ โญ & ๐ `yield` ๐ ๐ ๏ธ โญ ๐จ ๐จ:
```Python hl_lines="2-4"
{!../../../docs_src/dependencies/tutorial007.py!}
```
๐พ ๐ฒ โซ๏ธโ ๐ ๐ *โก ๐ ๏ธ* & ๐ ๐:
```Python hl_lines="4"
{!../../../docs_src/dependencies/tutorial007.py!}
```
๐ ๐ `yield` ๐ ๐ ๏ธ โฎ๏ธ ๐จ โ๏ธ ๐:
```Python hl_lines="5-6"
{!../../../docs_src/dependencies/tutorial007.py!}
```
!!! tip
๐ ๐ช โ๏ธ `async` โ๏ธ ๐ ๐ข.
**FastAPI** ๐ โถ๏ธ๏ธ ๐ โฎ๏ธ ๐ , ๐ โฎ๏ธ ๐ ๐.
## ๐ โฎ๏ธ `yield` & `try`
๐ฅ ๐ โ๏ธ `try` ๐ซ ๐ โฎ๏ธ `yield`, ๐ ๐ ๐จ ๐ โ ๐ ๐ฎ ๐โ โ๏ธ ๐.
๐ผ, ๐ฅ ๐ โ ๐, โ1๏ธโฃ ๐ โ๏ธ *โก ๐ ๏ธ*, โ ๐ฝ ๐ต "๐พ" โ๏ธ โ ๐ ๐ โ, ๐ ๐ ๐จ โ ๐ ๐.
, ๐ ๐ช ๐ ๐ ๐ฏ โ ๐ ๐ โฎ๏ธ `except SomeException`.
๐ ๐, ๐ ๐ช โ๏ธ `finally` โ ๐ญ ๐ช ๐ถ ๐ ๏ธ, ๐
โโ ๐ค ๐ฅ ๐ค โ โ๏ธ ๐ซ.
```Python hl_lines="3 5"
{!../../../docs_src/dependencies/tutorial007.py!}
```
## ๐ง-๐ โฎ๏ธ `yield`
๐ ๐ช โ๏ธ ๐ง-๐ & "๐ฒ" ๐ง-๐ ๐ ๐ & ๐ , & ๐ โ๏ธ ๐ ๐ซ ๐ช โ๏ธ `yield`.
**FastAPI** ๐ โ ๐ญ ๐ "๐ช ๐" ๐ ๐ โฎ๏ธ `yield` ๐ โ โ.
๐ผ, `dependency_c` ๐ช โ๏ธ ๐ ๐ `dependency_b`, & `dependency_b` ๐ `dependency_a`:
```Python hl_lines="4 12 20"
{!../../../docs_src/dependencies/tutorial008.py!}
```
& ๐ ๐ซ ๐ช โ๏ธ `yield`.
๐ ๐ผ `dependency_c`, ๐ ๏ธ ๐ฎ ๐ช ๐, ๐ช ๐ฒ โช๏ธโก๏ธ `dependency_b` (๐ฅ ๐ `dep_b`) ๐ช.
& , ๐, `dependency_b` ๐ช ๐ฒ โช๏ธโก๏ธ `dependency_a` (๐ฅ ๐ `dep_a`) ๐ช ๐ฎ ๐ช ๐.
```Python hl_lines="16-17 24-25"
{!../../../docs_src/dependencies/tutorial008.py!}
```
๐ ๐, ๐ ๐ช โ๏ธ ๐ โฎ๏ธ `yield` & `return` ๐.
& ๐ ๐ช โ๏ธ ๐ ๐ ๐ ๐ ๐ ๐ ๐ โฎ๏ธ `yield`, โ๏ธ.
๐ ๐ช โ๏ธ ๐ ๐ ๐ ๐ ๐ ๐.
**FastAPI** ๐ โ ๐ญ ๐ ๐ โ โ.
!!! note "๐ก โน"
๐ ๐ท ๐ ๐ ๐ ๐จโ๐ผ.
**FastAPI** โ๏ธ ๐ซ ๐ ๐ ๐.
## ๐ โฎ๏ธ `yield` & `HTTPException`
๐ ๐ ๐ ๐ ๐ช โ๏ธ ๐ โฎ๏ธ `yield` & โ๏ธ `try` ๐ซ ๐ โ โ .
โซ๏ธ 5๏ธโฃ๐ ๐ ๐ค `HTTPException` โ๏ธ ๐ ๐ช ๐, โฎ๏ธ `yield`. โ๏ธ **โซ๏ธ ๐ ๐ซ ๐ท**.
๐ช ๐ ๐ โฎ๏ธ `yield` ๐ ๏ธ *โฎ๏ธ* ๐จ ๐จ, [โ ๐โ๐ฆบ](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank} ๐ โ๏ธ โช ๐. ๐ค ๐ณ ๐ฝ โ ๐ฎ ๐ ๐ ๐ช ๐ (โฎ๏ธ `yield`).
, ๐ฅ ๐ ๐ค `HTTPException` โฎ๏ธ `yield`, ๐ข (โ๏ธ ๐ ๐) โ ๐โ๐ฆบ ๐ โ `HTTPException`โ & ๐จ ๐บ๐ธ๐ 4๏ธโฃ0๏ธโฃ0๏ธโฃ ๐จ ๐ ๐ซ ๐ค โ ๐ โ ๐ซ๐.
๐ โซ๏ธโ โ ๐ณ โ ๐ (โ
๐ฝ ๐), ๐ผ, โ๏ธ ๐ฅ ๐.
๐ฅ ๐ ๐ *โฎ๏ธ* ๐จ โ๏ธ ๐จ. ๐ค ๐
โโ ๐ ๐ค `HTTPException` โฉ๏ธ ๐ค ๐ซ ๐ ๐ ๐จ ๐ *โช ๐จ*.
โ๏ธ ๐ฅ ๐ฅ ๐ โ ๐ฝ โ, ๐ ๐ ๐ช ๐พ โ๏ธ ๐ฌ ๐ ๐ ๐ โฎ๏ธ `yield`, & ๐ฒ ๐น โ โ๏ธ ๐ โซ๏ธ ๐ฐ ๐ต โ๏ธ.
๐ฅ ๐ โ๏ธ ๐ ๐ ๐ ๐ญ ๐ช ๐ค โ , ๐ ๐/"๐" ๐ & ๐ฎ `try` ๐ซ ๐ ๐ ๐.
๐ฅ ๐ โ๏ธ ๐ โ ๐ ๐ ๐ ๐ ๐ต *โญ* ๐ฌ ๐จ & ๐ฒ โ ๐จ, ๐ฒ ๐โโ `HTTPException`, โ [๐ โ ๐โ๐ฆบ](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}.
!!! tip
๐ ๐ช ๐ค โ ๐ `HTTPException` *โญ* `yield`. โ๏ธ ๐ซ โฎ๏ธ.
๐ ๐ ๏ธ ๐
โ๏ธ ๐ ๐ ๐ ๐. ๐ฐ ๐ง โช๏ธโก๏ธ ๐ ๐. & ๐ ๐ 1๏ธโฃ ๐ ๐ โ๏ธ ๐ ๏ธ ๐.
```mermaid
sequenceDiagram
participant client as Client
participant handler as Exception handler
participant dep as Dep with yield
participant operation as Path Operation
participant tasks as Background tasks
Note over client,tasks: Can raise exception for dependency, handled after response is sent
Note over client,operation: Can raise HTTPException and can change the response
client ->> dep: Start request
Note over dep: Run code up to yield
opt raise
dep -->> handler: Raise HTTPException
handler -->> client: HTTP error response
dep -->> dep: Raise other exception
end
dep ->> operation: Run dependency, e.g. DB session
opt raise
operation -->> dep: Raise HTTPException
dep -->> handler: Auto forward exception
handler -->> client: HTTP error response
operation -->> dep: Raise other exception
dep -->> handler: Auto forward exception
end
operation ->> client: Return response to client
Note over client,operation: Response is already sent, can't change it anymore
opt Tasks
operation -->> tasks: Send background tasks
end
opt Raise other exception
tasks -->> dep: Raise other exception
end
Note over dep: After yield
opt Handle other exception
dep -->> dep: Handle exception, can't change response. E.g. close DB session.
end
```
!!! info
๐ด **1๏ธโฃ ๐จ** ๐ ๐จ ๐ฉโ๐ป. โซ๏ธ ๐ช 1๏ธโฃ โ ๐จ โ๏ธ โซ๏ธ ๐ ๐จ โช๏ธโก๏ธ *โก ๐ ๏ธ*.
โฎ๏ธ 1๏ธโฃ ๐ ๐จ ๐จ, ๐
โโ ๐ ๐จ ๐ช ๐จ.
!!! tip
๐ ๐ ๐ฆ `HTTPException`, โ๏ธ ๐ ๐ช ๐ค ๐ ๐ โ โ ๐ โ [๐ โ ๐โ๐ฆบ](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}.
๐ฅ ๐ ๐ค ๐ โ , โซ๏ธ ๐ ๐ถโโ๏ธ ๐ โฎ๏ธ ๐พ, ๐ `HTTPException`, & โคด๏ธ **๐** โ ๐โ๐ฆบ. ๐ฅ ๐ค ๐
โโ โ ๐โ๐ฆบ ๐ โ , โซ๏ธ ๐ โคด๏ธ ๐ต ๐ข ๐ `ServerErrorMiddleware`, ๐ฌ 5๏ธโฃ0๏ธโฃ0๏ธโฃ ๐บ๐ธ๐ ๐ ๐, โก๏ธ ๐ฉโ๐ป ๐ญ ๐ ๐ค โ ๐ฝ.
## ๐ ๐จโ๐ผ
### โซ๏ธโ "๐ ๐จโ๐ผ"
"๐ ๐จโ๐ผ" ๐ ๐ ๐ ๐ ๐ ๐ ๐ช โ๏ธ `with` ๐.
๐ผ, ๐ ๐ช โ๏ธ `with` โ ๐:
```Python
with open("./somefile.txt") as f:
contents = f.read()
print(contents)
```
๐, `open("./somefile.txt")` โ ๐ ๐ ๐ค "๐ ๐จโ๐ผ".
๐โ `with` ๐ซ ๐, โซ๏ธ โ ๐ญ ๐ ๐, ๐ฅ ๐ค โ .
๐โ ๐ โ ๐ โฎ๏ธ `yield`, **FastAPI** ๐ ๐ ๐ โซ๏ธ ๐ ๐จโ๐ผ, & ๐ โซ๏ธ โฎ๏ธ ๐ ๐ ๐งฐ.
### โ๏ธ ๐ ๐จโ๐ผ ๐ โฎ๏ธ `yield`
!!! warning
๐, ๐
โ๏ธ ๐, "๐ง" ๐ญ.
๐ฅ ๐ โถ๏ธ โฎ๏ธ **FastAPI** ๐ ๐ช ๐ ๐ถ โซ๏ธ ๐.
๐, ๐ ๐ช โ ๐ ๐จโ๐ผ ๐ ๐ โฎ๏ธ 2๏ธโฃ ๐ฉโ๐ฌ: `__enter__()` & `__exit__()`.
๐ ๐ช โ๏ธ ๐ซ ๐ **FastAPI** ๐ โฎ๏ธ `yield` โ๏ธ
`with` โ๏ธ `async with` ๐ ๐ ๐ ๐ข:
```Python hl_lines="1-9 13"
{!../../../docs_src/dependencies/tutorial010.py!}
```
!!! tip
โ1๏ธโฃ ๐ โ ๐ ๐จโ๐ผ โฎ๏ธ:
* `@contextlib.contextmanager` โ๏ธ
* `@contextlib.asynccontextmanager`
โ๏ธ ๐ซ ๐ ๐ข โฎ๏ธ ๐ `yield`.
๐ โซ๏ธโ **FastAPI** โ๏ธ ๐ ๐ โฎ๏ธ `yield`.
โ๏ธ ๐ ๐ซ โ๏ธ โ๏ธ ๐จโ๐จ FastAPI ๐ (& ๐ ๐ซ๐ ๐ซ).
FastAPI ๐ โซ๏ธ ๐ ๐.