Browse Source

๐ŸŒ Add Korean translation for `docs/ko/docs/tutorial/dependencies/dependencies-with-yield.md` (#13257)

pull/13339/head
11kkw 2 months ago
committed by GitHub
parent
commit
ad33193f2c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 275
      docs/ko/docs/tutorial/dependencies/dependencies-with-yield.md

275
docs/ko/docs/tutorial/dependencies/dependencies-with-yield.md

@ -0,0 +1,275 @@
# yield๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ
FastAPI๋Š” <abbr title='๋•Œ๋กœ๋Š” "์ข…๋ฃŒ ์ฝ”๋“œ", "์ •๋ฆฌ ์ฝ”๋“œ", "์ข…๋ฃŒ ์ฒ˜๋ฆฌ ์ฝ”๋“œ", "๋‹ซ๊ธฐ ์ฝ”๋“œ", "์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž ์ข…๋ฃŒ ์ฝ”๋“œ" ๋“ฑ์œผ๋กœ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค'>์ž‘์—… ์™„๋ฃŒ ํ›„ ์ถ”๊ฐ€ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š”</abbr> ์˜์กด์„ฑ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด `return` ๋Œ€์‹  `yield`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ถ”๊ฐ€๋กœ ์‹คํ–‰ํ•  ๋‹จ๊ณ„ (์ฝ”๋“œ)๋ฅผ ๊ทธ ๋’ค์— ์ž‘์„ฑํ•˜์„ธ์š”.
/// tip | ํŒ
๊ฐ ์˜์กด์„ฑ๋งˆ๋‹ค `yield`๋Š” ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
///
/// note | ๊ธฐ์ˆ  ์„ธ๋ถ€์‚ฌํ•ญ
๋‹ค์Œ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜:
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager" class="external-link" target="_blank">`@contextlib.contextmanager`</a> ๋˜๋Š”
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager" class="external-link" target="_blank">`@contextlib.asynccontextmanager`</a>
๋Š” **FastAPI**์˜ ์˜์กด์„ฑ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ์‹ค, FastAPI๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ด ๋‘ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
///
## `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์˜์กด์„ฑ
์˜ˆ๋ฅผ ๋“ค์–ด, ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ธ์…˜์„ ์ƒ์„ฑํ•˜๊ณ  ์ž‘์—…์ด ๋๋‚œ ํ›„์— ์„ธ์…˜์„ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์‘๋‹ต์„ ์ƒ์„ฑํ•˜๊ธฐ ์ „์—๋Š” `yield`๋ฌธ์„ ํฌํ•จํ•˜์—ฌ ๊ทธ ์ด์ „์˜ ์ฝ”๋“œ๋งŒ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค:
{* ../../docs_src/dependencies/tutorial007.py hl[2:4] *}
yield๋œ ๊ฐ’์€ *๊ฒฝ๋กœ ์ž‘์—…* ๋ฐ ๋‹ค๋ฅธ ์˜์กด์„ฑ๋“ค์— ์ฃผ์ž…๋˜๋Š” ๊ฐ’ ์ž…๋‹ˆ๋‹ค:
{* ../../docs_src/dependencies/tutorial007.py hl[4] *}
`yield`๋ฌธ ๋‹ค์Œ์˜ ์ฝ”๋“œ๋Š” ์‘๋‹ต์„ ์ƒ์„ฑํ•œ ํ›„ ๋ณด๋‚ด๊ธฐ ์ „์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค:
{* ../../docs_src/dependencies/tutorial007.py hl[5:6] *}
/// tip | ํŒ
`async` ํ•จ์ˆ˜์™€ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**FastAPI**๋Š” ์ผ๋ฐ˜ ์˜์กด์„ฑ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ๊ฐ์˜ ํ•จ์ˆ˜๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
///
## `yield`์™€ `try`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ
`yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์—์„œ `try` ๋ธ”๋ก์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๋„์ค‘ ๋ฐœ์ƒํ•œ ๋ชจ๋“  ์˜ˆ์™ธ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค๋ฅธ ์˜์กด์„ฑ์ด๋‚˜ *๊ฒฝ๋กœ ์ž‘์—…*์˜ ์ค‘๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํŠธ๋žœ์žญ์…˜ "๋กค๋ฐฑ"์ด ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ํ•ด๋‹น ์˜ˆ์™ธ๋ฅผ ์˜์กด์„ฑ์—์„œ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ, ์˜์กด์„ฑ ๋‚ด์—์„œ `except SomeException`์„ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, `finally`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์™ธ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„ ์—†์ด ์ข…๋ฃŒ ๋‹จ๊ณ„๊นŒ ์‹คํ–‰๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
{* ../../docs_src/dependencies/tutorial007.py hl[3,5] *}
## `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•˜์œ„ ์˜์กด์„ฑ
๋ชจ๋“  ํฌ๊ธฐ์™€ ํ˜•ํƒœ์˜ ํ•˜์œ„ ์˜์กด์„ฑ๊ณผ ํ•˜์œ„ ์˜์กด์„ฑ์˜ "ํŠธ๋ฆฌ"๋„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋“ค ๋ชจ๋‘๊ฐ€ `yield`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**FastAPI**๋Š” `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ ์˜์กด์„ฑ์˜ "์ข…๋ฃŒ ์ฝ”๋“œ"๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋กœ ์‹คํ–‰๋˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, `dependency_c`๋Š” `dependency_b`์— ์˜์กดํ•  ์ˆ˜ ์žˆ๊ณ , `dependency_b`๋Š” `dependency_a`์— ์˜์กดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
{* ../../docs_src/dependencies/tutorial008_an_py39.py hl[6,14,22] *}
์ด๋“ค ๋ชจ๋‘๋Š” `yield`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด ๊ฒฝ์šฐ `dependency_c`๋Š” ์ข…๋ฃŒ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด, `dependency_b`์˜ ๊ฐ’ (์—ฌ๊ธฐ์„œ๋Š” `dep_b`๋กœ ๋ช…๋ช…)์ด ์—ฌ์ „ํžˆ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ , `dependency_b`๋Š” ์ข…๋ฃŒ ์ฝ”๋“œ๋ฅผ ์œ„ํ•ด `dependency_a`์˜ ๊ฐ’ (์—ฌ๊ธฐ์„œ๋Š” `dep_a`๋กœ ๋ช…๋ช…) ์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
{* ../../docs_src/dependencies/tutorial008_an_py39.py hl[18:19,26:27] *}
๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ, `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ๊ณผ `return`์„ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋“ค ์ค‘ ์ผ๋ถ€๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ๋“ค์— ์˜์กดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ์˜์กด์„ฑ์„ ํ•„์š”๋กœ ํ•˜๋Š” ๋‹จ์ผ ์˜์กด์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
์›ํ•˜๋Š” ์˜์กด์„ฑ์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์กฐํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
**FastAPI**๋Š” ๋ชจ๋“  ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋กœ ์‹คํ–‰๋˜๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
/// note | ๊ธฐ์ˆ  ์„ธ๋ถ€์‚ฌํ•ญ
ํŒŒ์ด์ฌ์˜ <a href=โ€œhttps://docs.python.org/3/library/contextlib.htmlโ€ class=โ€œexternal-linkโ€ target=โ€œ_blankโ€>Context Managers</a> ๋•๋ถ„์— ์ด ๊ธฐ๋Šฅ์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
**FastAPI**๋Š” ์ด๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
///
## `yield`์™€ `HTTPException`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ
`yield`์™€ `try` ๋ธ”๋ก์ด ์žˆ๋Š” ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ, `yield` ์ดํ›„์˜ ์ข…๋ฃŒ ์ฝ”๋“œ์—์„œ `HTTPException`์ด๋‚˜ ์œ ์‚ฌํ•œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
/// tip | ํŒ
์ด๋Š” ๋‹ค์†Œ ๊ณ ๊ธ‰ ๊ธฐ์ˆ ์ด๋ฉฐ, ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๊ฒฝ๋กœ ์—ฐ์‚ฐ ํ•จ์ˆ˜ ๋“ฑ ๋‚˜๋จธ์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ ๋‚ด๋ถ€์—์„œ ์˜ˆ์™ธ (`HTTPException` ํฌํ•จ)๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์‹ค์ œ๋กœ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿค“
///
{* ../../docs_src/dependencies/tutorial008b_an_py39.py hl[18:22,31] *}
์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ (๋˜๋Š” ์ถ”๊ฐ€๋กœ ๋‹ค๋ฅธ `HTTPException`์„ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด) ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ [์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ธฐ](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
## `yield`์™€ `except`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ
`yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์—์„œ `except`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•˜๊ณ  ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ฑฐ๋‚˜ (๋˜๋Š” ์ƒˆ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด), FastAPI๋Š” ํ•ด๋‹น ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋Š” ์ผ๋ฐ˜์ ์ธ Python ๋ฐฉ์‹๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค:
{* ../../docs_src/dependencies/tutorial008c_an_py39.py hl[15:16] *}
์ด ๊ฒฝ์šฐ, `HTTPException`์ด๋‚˜ ์œ ์‚ฌํ•œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ๋Š” HTTP 500 Internal Server Error ์‘๋‹ต์„ ๋ณด๊ฒŒ ๋˜์ง€๋งŒ, ์„œ๋ฒ„๋Š” ์–ด๋–ค ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€์— ๋Œ€ํ•œ **๋กœ๊ทธ**๋‚˜ ๋‹ค๋ฅธ ํ‘œ์‹œ๋ฅผ ์ „ํ˜€ ๊ฐ€์ง€์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๐Ÿ˜ฑ
### `yield`์™€ `except`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์—์„œ ํ•ญ์ƒ `raise` ํ•˜๊ธฐ
`yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ์—์„œ ์˜ˆ์™ธ๋ฅผ ์žก์•˜์„ ๋•Œ๋Š” `HTTPException`์ด๋‚˜ ์œ ์‚ฌํ•œ ์˜ˆ์™ธ๋ฅผ ์ƒˆ๋กœ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š” ํ•œ, ๋ฐ˜๋“œ์‹œ ์›๋ž˜์˜ ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
`raise`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
{* ../../docs_src/dependencies/tutorial008d_an_py39.py hl[17] *}
์ด์ œ ํด๋ผ์ด์–ธํŠธ๋Š” ๋™์ผํ•œ *HTTP 500 Internal Server Error* ์˜ค๋ฅ˜ ์‘๋‹ต์„ ๋ฐ›๊ฒŒ ๋˜์ง€๋งŒ, ์„œ๋ฒ„ ๋กœ๊ทธ์—๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ์ธ `InternalError"๊ฐ€ ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค. ๐Ÿ˜Ž
## `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์˜ ์‹คํ–‰ ์ˆœ์„œ
์‹คํ–‰ ์ˆœ์„œ๋Š” ์•„๋ž˜ ๋‹ค์ด์–ด๊ทธ๋žจ๊ณผ ๊ฑฐ์˜ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„์€ ์œ„์—์„œ ์•„๋ž˜๋กœ ํ๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์—ด์€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๊ฑฐ๋‚˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ถ€๋ถ„ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
```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,operation: Can raise exceptions, including HTTPException
client ->> dep: Start request
Note over dep: Run code up to yield
opt raise Exception
dep -->> handler: Raise Exception
handler -->> client: HTTP error response
end
dep ->> operation: Run dependency, e.g. DB session
opt raise
operation -->> dep: Raise Exception (e.g. HTTPException)
opt handle
dep -->> dep: Can catch exception, raise a new HTTPException, raise other exception
end
handler -->> client: HTTP error response
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 -->> tasks: Handle exceptions in the background task code
end
```
/// info | ์ •๋ณด
ํด๋ผ์ด์–ธํŠธ์— **ํ•˜๋‚˜์˜ ์‘๋‹ต** ๋งŒ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์˜ค๋ฅ˜ ์‘๋‹ต ์ค‘ ํ•˜๋‚˜์ผ ์ˆ˜๋„ ์žˆ๊ณ ,*๊ฒฝ๋กœ ์ž‘์—…*์—์„œ ์ƒ์„ฑ๋œ ์‘๋‹ต์ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์‘๋‹ต ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ „์†ก๋œ ํ›„์—๋Š” ๋‹ค๋ฅธ ์‘๋‹ต์„ ๋ณด๋‚ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
///
/// tip | ํŒ
์ด ๋‹ค์ด์–ด๊ทธ๋žจ์€ `HTTPException`์„ ๋ณด์—ฌ์ฃผ์ง€๋งŒ, `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์—์„œ ์ฒ˜๋ฆฌํ•œ ์˜ˆ์™ธ๋‚˜ [์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ธฐ](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}.๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•œ ๋‹ค๋ฅธ ์˜ˆ์™ธ๋„ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์–ด๋–ค ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋“ , `HTTPException`์„ ํฌํ•จํ•˜์—ฌ yield๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค๊ฑฐ๋‚˜ ์ƒˆ๋กœ์šด ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
///
## `yield`, `HTTPException`, `except` ๋ฐ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์„ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ
/// warning | ๊ฒฝ๊ณ 
์ด๋Ÿฌํ•œ ๊ธฐ์ˆ ์  ์„ธ๋ถ€ ์‚ฌํ•ญ์€ ๋Œ€๋ถ€๋ถ„ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ด ์„น์…˜์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ์•„๋ž˜์—์„œ ๊ณ„์† ์ง„ํ–‰ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์„ธ๋ถ€ ์ •๋ณด๋Š” ์ฃผ๋กœ FastAPI 0.106.0 ์ด์ „ ๋ฒ„์ „์—์„œ `yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๊ฒฝ์šฐ๋ฉ” ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
///
### `yield`์™€ `except`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ, ๊ธฐ์ˆ  ์„ธ๋ถ€์‚ฌํ•ญ
FastAPI 0.110.0 ์ด์ „์—๋Š” `yield`๊ฐ€ ํฌํ•จ๋œ ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•œ ํ›„ ํ•ด๋‹น ์˜์กด์„ฑ์—์„œ `except`๊ฐ€ ํฌํ•จ๋œ ์˜ˆ์™ธ๋ฅผ ์บก์ฒ˜ํ•˜๊ณ  ๋‹ค์‹œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ๊ฐ€ ์ž๋™์œผ๋กœ ์˜ˆ์™ธ ํ•ธ๋“ค๋Ÿฌ ๋˜๋Š” ๋‚ด๋ถ€ ์„œ๋ฒ„ ์˜ค๋ฅ˜ ํ•ธ๋“ค๋Ÿฌ๋กœ ๋ฐœ์ƒ/์ „๋‹ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ด๋Š” ์ฒ˜๋ฆฌ๊ธฐ ์—†์ด ์ „๋‹ฌ๋œ ์˜ˆ์™ธ(๋‚ด๋ถ€ ์„œ๋ฒ„ ์˜ค๋ฅ˜)์—์„œ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์ผ๋ฐ˜ ํŒŒ์ด์ฌ ์ฝ”๋“œ์˜ ๋™์ž‘๊ณผ ์ผ์น˜ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด 0.110.0 ๋ฒ„์ „์—์„œ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
### ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…๊ณผ `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ, ๊ธฐ์ˆ  ์„ธ๋ถ€์‚ฌํ•ญ
FastAPI 0.106.0 ์ด์ „์—๋Š” `yield` ์ดํ›„์— ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ–ˆ์Šต๋‹ˆ๋‹ค. `yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ ์ข…๋ฃŒ ์ฝ”๋“œ๋Š” ์‘๋‹ต์ด ์ „์†ก๋œ ์ดํ›„์— ์‹คํ–‰๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, [์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ธฐ](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}๊ฐ€ ์ด๋ฏธ ์‹คํ–‰๋œ ์ƒํƒœ์˜€์Šต๋‹ˆ๋‹ค.
์ด๋Š” ์ฃผ๋กœ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๋‚ด์—์„œ ์˜์กด์„ฑ์—์„œ "yield๋œ" ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ข…๋ฃŒ ์ฝ”๋“œ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ํ›„์— ์‹คํ–‰๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค
ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฆฌ์†Œ์Šค๋ฅผ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์–‘๋ณดํ•œ ์˜์กด์„ฑ(์˜ˆ: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ)์—์„œ ๋ณด์œ ํ•˜๋ฉด์„œ ์‘๋‹ต์ด ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ด๋™ํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๊ธฐ ๋•Œ๋ฌธ์— FastAPI 0.106.0์—์„œ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
/// tip | ํŒ
๋˜ํ•œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์ž์ฒด ๋ฆฌ์†Œ์Šค(์˜ˆ: ์ž์ฒด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ„๋„๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๋…๋ฆฝ์ ์ธ ๋กœ์ง ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ๊น”๋”ํ•ด์ง‘๋‹ˆ๋‹ค.
///
๋งŒ์•ฝ ์ด์ „์— ์ด๋Ÿฌํ•œ ๋™์ž‘์— ์˜์กดํ–ˆ๋‹ค๋ฉด, ์ด์ œ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๋‚ด๋ถ€์—์„œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—…์„ ์œ„ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , `yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ์˜ ๋ฆฌ์†Œ์Šค์— ์˜์กดํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ธ์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹ , ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ๋‚ด๋ถ€์—์„œ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ธ์…˜์„ ์ƒ์„ฑํ•˜๊ณ  ์ด ์ƒˆ๋กœ์šด ์„ธ์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด๋ฅผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง์ ‘ ์ „๋‹ฌํ•˜๋Š” ๋Œ€์‹ , ํ•ด๋‹น ๊ฐ์ฒด์˜ ID๋ฅผ ์ „๋‹ฌํ•œ ๋‹ค์Œ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๊ฐ€์ ธ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค
## ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž
### "์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž"๋ž€?
"์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž"๋Š” Python์—์„œ `with` ๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, <a href="https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files" class="external-link" target="_blank"> `with`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค</a>:
```Python
with open("./somefile.txt") as f:
contents = f.read()
print(contents)
```
๋‚ด๋ถ€์ ์œผ๋กœ `open("./somefile.txt")` ๋Š” "์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž(Context Manager)"๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
`with` ๋ธ”๋ก์ด ๋๋‚˜๋ฉด, ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋”๋ผ๋„ ํŒŒ์ผ์„ ๋‹ซ๋„๋ก ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.
`yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ์„ ์ƒ์„ฑํ•˜๋ฉด **FastAPI**๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ์ด๋ฅผ ์œ„ํ•œ ์ปจํ…์ŠคํŠธ ๋งค๋‹ˆ์ €๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค๋ฅธ ๊ด€๋ จ ๋„๊ตฌ๋“ค๊ณผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
### `yield`๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜์กด์„ฑ์—์„œ ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž ์‚ฌ์šฉํ•˜๊ธฐ
/// warning | ๊ฒฝ๊ณ 
์ด๊ฒƒ์€ ์–ด๋Š ์ •๋„ "๊ณ ๊ธ‰" ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.
**FastAPI**๋ฅผ ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ ์ง€๊ธˆ์€ ์ด ๋ถ€๋ถ„์„ ๊ฑด๋„ˆ๋›ฐ์–ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค.
///
Python์—์„œ๋Š” ๋‹ค์Œ์„ ํ†ตํ•ด ์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. <a href="https://docs.python.org/3/reference/datamodel.html#context-managers" class="external-link" target="_blank"> ๋‘ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค: `__enter__()` and `__exit__()`</a>.
**FastAPI**์˜ `yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ ๋‚ด์—์„œ
`with` ๋˜๋Š” `async with`๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋“ค์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
{* ../../docs_src/dependencies/tutorial010.py hl[1:9,13] *}
/// tip | ํŒ
์ปจํ…์ŠคํŠธ ๊ด€๋ฆฌ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager" class="external-link" target="_blank">`@contextlib.contextmanager`</a> ๋˜๋Š”
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager" class="external-link" target="_blank">`@contextlib.asynccontextmanager`</a>
์ด๋“ค์€ ๋‹จ์ผ `yield`๊ฐ€ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๊พธ๋ฏธ๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์ด๊ฒƒ์ด **FastAPI**๊ฐ€ `yield`๊ฐ€ ์žˆ๋Š” ์˜์กด์„ฑ์„ ์œ„ํ•ด ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ FastAPI ์˜์กด์„ฑ์—๋Š” ์ด๋Ÿฌํ•œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค(๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉํ•ด์„œ๋„ ์•ˆ๋ฉ๋‹ˆ๋‹ค).
FastAPI๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•ด ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
///
Loadingโ€ฆ
Cancel
Save