Skip to content

Commit

Permalink
🌐 Add Korean translation for `docs/ko/docs/tutorial/background-tasks.…
Browse files Browse the repository at this point in the history
…md` (#5910)
  • Loading branch information
junah201 committed Jan 27, 2024
1 parent 2f2a7ad commit 3817514
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions docs/ko/docs/tutorial/background-tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…

FastAPIμ—μ„œλŠ” 응닡을 λ°˜ν™˜ν•œ 후에 μ‹€ν–‰ν•  λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ„ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ€ ν΄λΌμ΄μ–ΈνŠΈκ°€ 응닡을 λ°›κΈ° μœ„ν•΄ μž‘μ—…μ΄ μ™„λ£Œλ  λ•ŒκΉŒμ§€ 기닀릴 ν•„μš”κ°€ μ—†κΈ° λ•Œλ¬Έμ— μš”μ²­ 후에 λ°œμƒν•΄μ•Όν•˜λŠ” μž‘μ—…μ— 맀우 μœ μš©ν•©λ‹ˆλ‹€.

μ΄λŸ¬ν•œ μž‘μ—…μ—λŠ” λ‹€μŒμ΄ ν¬ν•¨λ©λ‹ˆλ‹€.

* μž‘μ—…μ„ μˆ˜ν–‰ν•œ ν›„ μ „μ†‘λ˜λŠ” 이메일 μ•Œλ¦Ό
* 이메일 μ„œλ²„μ— μ—°κ²°ν•˜κ³  이메일을 μ „μ†‘ν•˜λŠ” 것은 (λͺ‡ 초 정도) "느린" κ²½ν–₯이 μžˆμœΌλ―€λ‘œ, 응닡은 μ¦‰μ‹œ λ°˜ν™˜ν•˜κ³  이메일 μ•Œλ¦Όμ€ λ°±κ·ΈλΌμš΄λ“œμ—μ„œ μ „μ†‘ν•˜λŠ” 게 κ°€λŠ₯ν•©λ‹ˆλ‹€.
* 데이터 처리:
* 예λ₯Ό λ“€μ–΄ μ²˜λ¦¬μ— 였랜 μ‹œκ°„μ΄ κ±Έλ¦¬λŠ” 데이터λ₯Ό λ°›μ•˜μ„ λ•Œ "Accepted" (HTTP 202)을 λ°˜ν™˜ν•˜κ³ , λ°±κ·ΈλΌμš΄λ“œμ—μ„œ 데이터λ₯Ό μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

## `λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…` μ‚¬μš©

λ¨Όμ € μ•„λž˜μ™€ 같이 `BackgroundTasks`λ₯Ό μž„ν¬νŠΈν•˜κ³ , `BackgroundTasks`λ₯Ό _경둜 λ™μž‘ ν•¨μˆ˜_ μ—μ„œ λ§€κ°œλ³€μˆ˜λ‘œ κ°€μ Έμ˜€κ³  μ •μ˜ν•©λ‹ˆλ‹€.

```Python hl_lines="1 13"
{!../../../docs_src/background_tasks/tutorial001.py!}
```

**FastAPI** λŠ” `BackgroundTasks` 개체λ₯Ό μƒμ„±ν•˜κ³ , 맀개 λ³€μˆ˜λ‘œ μ „λ‹¬ν•©λ‹ˆλ‹€.

## μž‘μ—… ν•¨μˆ˜ 생성

λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μœΌλ‘œ μ‹€ν–‰ν•  ν•¨μˆ˜λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.

이것은 λ‹¨μˆœνžˆ λ§€κ°œλ³€μˆ˜λ₯Ό 받을 수 μžˆλŠ” ν‘œμ€€ ν•¨μˆ˜μΌ λΏμž…λ‹ˆλ‹€.

**FastAPI**λŠ” 이것이 `async def` ν•¨μˆ˜μ΄λ“ , 일반 `def` ν•¨μˆ˜μ΄λ“  λ‚΄λΆ€μ μœΌλ‘œ 이λ₯Ό μ˜¬λ°”λ₯΄κ²Œ μ²˜λ¦¬ν•©λ‹ˆλ‹€.

이 경우, μ•„λž˜ μž‘μ—…μ€ νŒŒμΌμ— μ“°λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. (이메일 보내기 μ‹œλ¬Όλ ˆμ΄μ…˜)

그리고 이 μž‘μ—…μ€ `async`와 `await`λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 일반 `def` ν•¨μˆ˜λ‘œ μ„ μ–Έν•©λ‹ˆλ‹€.

```Python hl_lines="6-9"
{!../../../docs_src/background_tasks/tutorial001.py!}
```

## λ°±κ·ΈλΌμš΄λ“œ μž‘μ—… μΆ”κ°€

_경둜 λ™μž‘ ν•¨μˆ˜_ λ‚΄μ—μ„œ μž‘μ—… ν•¨μˆ˜λ₯Ό `.add_task()` ν•¨μˆ˜ 톡해 _λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…_ κ°œμ²΄μ— μ „λ‹¬ν•©λ‹ˆλ‹€.

```Python hl_lines="14"
{!../../../docs_src/background_tasks/tutorial001.py!}
```

`.add_task()` ν•¨μˆ˜λŠ” λ‹€μŒκ³Ό 같은 인자λ₯Ό λ°›μŠ΅λ‹ˆλ‹€ :

- λ°±κ·ΈλΌμš΄λ“œμ—μ„œ μ‹€ν–‰λ˜λŠ” μž‘μ—… ν•¨μˆ˜ (`write_notification`).
- μž‘μ—… ν•¨μˆ˜μ— μˆœμ„œλŒ€λ‘œ μ „λ‹¬λ˜μ–΄μ•Ό ν•˜λŠ” 일련의 인자 (`email`).
- μž‘μ—… ν•¨μˆ˜μ— μ „λ‹¬λ˜μ–΄μ•Όν•˜λŠ” λͺ¨λ“  ν‚€μ›Œλ“œ 인자 (`message="some notification"`).

## μ˜μ‘΄μ„± μ£Όμž…

`BackgroundTasks`λ₯Ό μ˜μ‘΄μ„± μ£Όμž… μ‹œμŠ€ν…œκ³Ό ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ _경둜 λ™μž‘ ν•¨μˆ˜_, 쒅속성, ν•˜μœ„ 쒅속성 λ“± μ—¬λŸ¬ μˆ˜μ€€μ—μ„œ BackgroundTasks μœ ν˜•μ˜ λ§€κ°œλ³€μˆ˜λ₯Ό μ„ μ–Έν•  수 μžˆμŠ΅λ‹ˆλ‹€.

**FastAPI**λŠ” 각 κ²½μš°μ— μˆ˜ν–‰ν•  μž‘μ—…κ³Ό λ™μΌν•œ 개체λ₯Ό λ‚΄λΆ€μ μœΌλ‘œ μž¬μ‚¬μš©ν•˜κΈ°μ—, λͺ¨λ“  λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ΄ ν•¨κ»˜ λ³‘ν•©λ˜κ³  λ‚˜μ€‘μ— λ°±κ·ΈλΌμš΄λ“œμ—μ„œ μ‹€ν–‰λ©λ‹ˆλ‹€.

=== "Python 3.6 and above"

```Python hl_lines="13 15 22 25"
{!> ../../../docs_src/background_tasks/tutorial002.py!}
```

=== "Python 3.10 and above"

```Python hl_lines="11 13 20 23"
{!> ../../../docs_src/background_tasks/tutorial002_py310.py!}
```

이 μ˜ˆμ œμ—μ„œλŠ” 응닡이 λ°˜ν™˜λœ 후에 `log.txt` νŒŒμΌμ— λ©”μ‹œμ§€κ°€ κΈ°λ‘λ©λ‹ˆλ‹€.

μš”μ²­μ— 쿼리가 μžˆλŠ” 경우 λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ˜ λ‘œκ·Έμ— κΈ°λ‘λ©λ‹ˆλ‹€.

그리고 _경둜 λ™μž‘ ν•¨μˆ˜_ μ—μ„œ μƒμ„±λœ 또 λ‹€λ₯Έ λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ€ 경둜 맀개 λ³€μˆ˜λ₯Ό ν™œμš©ν•˜μ—¬ μ‚¬μš©ν•˜μ—¬ λ©”μ‹œμ§€λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.

## 기술적 세뢀사항

`BackgroundTasks` ν΄λž˜μŠ€λŠ” <a href="https://www.starlette.io/background/" class="external-link" target="_blank">`starlette.background`</a>μ—μ„œ 직접 κ°€μ Έμ˜΅λ‹ˆλ‹€.

`BackgroundTasks` ν΄λž˜μŠ€λŠ” FastAPIμ—μ„œ 직접 μž„ν¬νŠΈν•˜κ±°λ‚˜ ν¬ν•¨ν•˜κΈ° λ•Œλ¬Έμ— μ‹€μˆ˜λ‘œ `BackgroundTask` (끝에 `s`κ°€ μ—†μŒ)을 μž„ν¬νŠΈν•˜λ”λΌλ„ starlette.backgroundμ—μ„œ `BackgroundTask`λ₯Ό κ°€μ Έμ˜€λŠ” 것을 방지할 수 μžˆμŠ΅λ‹ˆλ‹€.

(`BackgroundTask`κ°€ μ•„λ‹Œ) `BackgroundTasks`λ₯Ό μ‚¬μš©ν•˜λ©΄, _경둜 λ™μž‘ ν•¨μˆ˜_ λ§€κ°œλ³€μˆ˜λ‘œ μ‚¬μš©ν•  수 있게 되고 λ‚˜λ¨Έμ§€λŠ” **FastAPI**κ°€ λŒ€μ‹  μ²˜λ¦¬ν•˜λ„λ‘ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이것은 `Request` 객체λ₯Ό 직접 μ‚¬μš©ν•˜λŠ” 것과 같은 λ°©μ‹μž…λ‹ˆλ‹€.

FastAPIμ—μ„œ `BackgroundTask`λ₯Ό λ‹¨λ…μœΌλ‘œ μ‚¬μš©ν•˜λŠ” 것은 μ—¬μ „νžˆ κ°€λŠ₯ν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ 객체λ₯Ό μ½”λ“œμ—μ„œ μƒμ„±ν•˜κ³ , 이 객체λ₯Ό ν¬ν•¨ν•˜λŠ” Starlette `Response`λ₯Ό λ°˜ν™˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

<a href="https://www.starlette.io/background/" class="external-link" target="_blank">`Starlette의 곡식 λ¬Έμ„œ`</a>μ—μ„œ λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ— λŒ€ν•œ μžμ„Έν•œ λ‚΄μš©μ„ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

## κ²½κ³ 

λ§Œμ•½ 무거운 λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ„ μˆ˜ν–‰ν•΄μ•Όν•˜κ³  λ™μΌν•œ ν”„λ‘œμ„ΈμŠ€μ—μ„œ μ‹€ν–‰ν•  ν•„μš”κ°€ μ—†λŠ” 경우 (예: λ©”λͺ¨λ¦¬, λ³€μˆ˜ 등을 κ³΅μœ ν•  ν•„μš”κ°€ μ—†μŒ) <a href="https://docs.celeryq.dev" class="external-link" target="_blank">`Celery`</a>와 같은 큰 도ꡬλ₯Ό μ‚¬μš©ν•˜λ©΄ 도움이 될 수 μžˆμŠ΅λ‹ˆλ‹€.

RabbitMQ λ˜λŠ” Redis와 같은 λ©”μ‹œμ§€/μž‘μ—… 큐 μ‹œμŠ€ν…œ 보닀 λ³΅μž‘ν•œ ꡬ성이 ν•„μš”ν•œ κ²½ν–₯이 μžˆμ§€λ§Œ, μ—¬λŸ¬ μž‘μ—… ν”„λ‘œμ„ΈμŠ€λ₯Ό 특히 μ—¬λŸ¬ μ„œλ²„μ˜ λ°±κ·ΈλΌμš΄λ“œμ—μ„œ μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

예제λ₯Ό λ³΄μ‹œλ €λ©΄ [ν”„λ‘œμ νŠΈ 생성기](../project-generation.md){.internal-link target=\_blank} λ₯Ό μ°Έκ³ ν•˜μ„Έμš”. ν•΄λ‹Ή μ˜ˆμ œμ—λŠ” 이미 κ΅¬μ„±λœ `Celery`κ°€ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ λ™μΌν•œ FastAPI μ•±μ—μ„œ λ³€μˆ˜ 및 κ°œμ²΄μ— μ ‘κ·Όν•΄μ•Όν–λŠ” μž‘μ€ λ°±κ·ΈλΌμš΄λ“œ μˆ˜ν–‰μ΄ ν•„μš”ν•œ 경우 (예 : μ•Œλ¦Ό 이메일 보내기) κ°„λ‹¨ν•˜κ²Œ `BackgroundTasks`λ₯Ό μ‚¬μš©ν•΄λ³΄μ„Έμš”.

## μš”μ•½

λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…μ„ μΆ”κ°€ν•˜κΈ° μœ„ν•΄ _경둜 λ™μž‘ ν•¨μˆ˜_ 에 λ§€κ°œλ³€μˆ˜λ‘œ `BackgroundTasks`λ₯Ό κ°€μ Έμ˜€κ³  μ‚¬μš©ν•©λ‹ˆλ‹€.

0 comments on commit 3817514

Please sign in to comment.