Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌐 Add Japanese translation for docs/ja/docs/advanced/events.md #5270

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/ja/docs/advanced/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# イベント: 起動時 - シャットダウン時

アプリケーションの起動前やシャットダウン時に実行する必要のあるイベントハンドラ(関数)を定義することができます。

これらの関数は、 `async def` または通常の `def` で宣言することができます。

!!! warning "警告"
メインアプリケーションのイベントハンドラのみが実行され、[サブアプリケーション - マウント](./sub-applications.md){.internal-link target=_blank}では実行されません。

## `startup` イベント

アプリケーションが起動する前に実行されるべき関数を追加するには、イベント `"startup"` と共に宣言します。

```Python hl_lines="8"
{!../../../docs_src/events/tutorial001.py!}
```

この場合、`startup`イベントハンドラ関数は、いくつかの値でアイテムの "データベース"(単なる`dict`)を初期化します。

イベントハンドラ関数は、複数追加することができます。

そして、すべての `startup` イベントハンドラが完了するまで、アプリケーションはリクエストを受け取り始めません。

## `shutdown` イベント

アプリケーションがシャットダウンするときに実行されるべき関数を追加するには、イベント `"shutdown"` と共に宣言します。

```Python hl_lines="6"
{!../../../docs_src/events/tutorial002.py!}
```

ここでは、`shutdown` イベントハンドラ関数が `"Application shutdown"` というテキスト行をファイル `log.txt` に書き出します。

!!! info "情報"
`open()`関数の中の、`mode="a"` は「追加」を意味し、そのファイルの前の内容に上書きすることなく、あるものの後に行を追加します。

!!! note "技術詳細"
この場合、ファイルとインタラクトする標準的なPythonの `open()` 関数を使用していることに注意してください。

つまり、I/O (入力/出力)を伴うため、ディスクに書き込まれるのを「待つ」必要があります。

しかし、`open()` は `async` と `await` を使えません。

そのため、イベントハンドラ関数を `async def` ではなく、標準の `def` で宣言します。

!!! info "情報"
これらのイベントハンドラについて、詳しくは <a href="https://www.starlette.io/events/" class="external-link" target="_blank">Starletteのイベントのドキュメント</a>をご覧ください。