<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>fallpoke</author_name>
  <author_url>https://blog.hatena.ne.jp/fallpoke/</author_url>
  <blog_title>誰かの技術置き場</blog_title>
  <blog_url>https://fallpoke-tech.hatenadiary.jp/</blog_url>
  <categories>
    <anon>FastAPI</anon>
    <anon>Python</anon>
    <anon>Webアプリ</anon>
    <anon>備忘録</anon>
  </categories>
  <description>お疲れ様です。 FastAPIでlifespanという機能を使う機会があったのでメモ。 lifespanの公式ドキュメントはこちら。 fastapi.tiangolo.com 機能の内容をざっくりというとAPIの起動時と終了時の処理を管理するというものです。 コードは以下のようになり、yieldの前に起動時の処理、後に終了時の処理を書きます。 また、yieldにデータを渡すことでリソースの共有も可能です。 以前記事にしたapp.state と似たような使い方ができます。 FastAPIのappを定義する前に設定して、定義時に引数で与えるという形になります。 @asynccontextmanag…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Ffallpoke-tech.hatenadiary.jp%2Fentry%2F2025%2F09%2F02%2F231908&quot; title=&quot;【FastAPI】開始時と終了時に処理を管理するlifespanについて【備忘録】 - 誰かの技術置き場&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url>https://cdn-ak.f.st-hatena.com/images/fotolife/f/fallpoke/20250902/20250902230744.png</image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2025-09-02 23:19:08</published>
  <title>【FastAPI】開始時と終了時に処理を管理するlifespanについて【備忘録】</title>
  <type>rich</type>
  <url>https://fallpoke-tech.hatenadiary.jp/entry/2025/09/02/231908</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
