<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Asgi on K-Life Hack | Systems Architecture &amp; DevOps</title><link>https://klifehack.com/en/tags/asgi/</link><description>Recent content in Asgi on K-Life Hack | Systems Architecture &amp; DevOps</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 03 Jun 2026 09:08:46 +0900</lastBuildDate><atom:link href="https://klifehack.com/en/tags/asgi/index.xml" rel="self" type="application/rss+xml"/><item><title>Technical Considerations on the Evolution of Python Web Interfaces from CGI to ASGI and Modern WAS Configurations</title><link>https://klifehack.com/en/p/python-web-interface-cgi-wsgi-asgi/</link><pubDate>Wed, 03 Jun 2026 09:08:46 +0900</pubDate><guid>https://klifehack.com/en/p/python-web-interface-cgi-wsgi-asgi/</guid><description>&lt;p&gt;Web application interface standards in Python have evolved from the early CGI to WSGI, and now to modern ASGI. This article provides a technical analysis of the operating principles, performance characteristics of each protocol, and the role of the WAS (Web Application Server) in modern infrastructure.&lt;/p&gt;
&lt;h2 id="1-structure-and-limitations-of-cgi-common-gateway-interface"&gt;1. Structure and Limitations of CGI (Common Gateway Interface)
&lt;/h2&gt;&lt;p&gt;CGI is the earliest standard protocol for web servers to interact with external programs. Its core lies in the &amp;ldquo;process-per-request&amp;rdquo; model.&lt;/p&gt;
&lt;h3 id="-operating-logic"&gt;💡 Operating Logic
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;The web server receives an HTTP request.&lt;/li&gt;
&lt;li&gt;The server forks a new OS process for each request, executing the Python interpreter and script.&lt;/li&gt;
&lt;li&gt;The script writes the result to standard output (stdout), and the process terminates.&lt;/li&gt;
&lt;li&gt;The server returns that output to the client as an HTTP response.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="-technical-challenges"&gt;⚠️ Technical Challenges
&lt;/h3&gt;&lt;p&gt;Because this model loads the interpreter and initializes the environment for every request, the overhead is extremely high, making it unsuitable for operation in high-traffic environments. While safety is ensured through process isolation, it is rarely adopted in modern systems due to resource efficiency concerns.&lt;/p&gt;
&lt;h2 id="2-optimization-via-wsgi-web-server-gateway-interface"&gt;2. Optimization via WSGI (Web Server Gateway Interface)
&lt;/h2&gt;&lt;p&gt;WSGI was formulated to resolve the overhead of CGI. WSGI provides a standard interface that keeps the Python application in memory as a persistent process to handle requests.&lt;/p&gt;
&lt;h3 id="-key-implementation-points"&gt;🛠️ Key Implementation Points
&lt;/h3&gt;&lt;p&gt;In WSGI, the application is defined as a &amp;ldquo;callable object (Callable)&amp;rdquo;. Once the server loads this object, it can call it repeatedly without restarting the process.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;application&lt;/span&gt;(environ, start_response):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; status &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;200 OK&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; headers &lt;span style="color:#f92672"&gt;=&lt;/span&gt; [(&lt;span style="color:#e6db74"&gt;&amp;#39;Content-Type&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;text/plain; charset=utf-8&amp;#39;&lt;/span&gt;)]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; start_response(status, headers)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;b&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;Hello, WSGI World&amp;#34;&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In current production environments, a configuration placing Nginx as a reverse proxy and Gunicorn as the WSGI server (WAS) is common.&lt;/p&gt;
&lt;h2 id="3-transition-to-asgi-asynchronous-server-gateway-interface"&gt;3. Transition to ASGI (Asynchronous Server Gateway Interface)
&lt;/h2&gt;&lt;p&gt;Since WSGI is designed assuming a synchronous request-response cycle, it has limitations in handling modern asynchronous communications such as WebSockets, Long Polling, and HTTP2. ASGI emerged to resolve this.&lt;/p&gt;
&lt;h3 id="-characteristics-of-asgi"&gt;💡 Characteristics of ASGI
&lt;/h3&gt;&lt;p&gt;While inheriting the spirit of WSGI, ASGI natively supports Python&amp;rsquo;s &lt;code&gt;async/await&lt;/code&gt; syntax. This makes it possible to efficiently manage thousands of concurrent connections in a single process using asynchronous I/O.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;application&lt;/span&gt;(scope, receive, send):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; scope[&lt;span style="color:#e6db74"&gt;&amp;#39;type&amp;#39;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;==&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;http&amp;#39;&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;await&lt;/span&gt; send({
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;type&amp;#39;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#39;http.response.start&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;status&amp;#39;&lt;/span&gt;: &lt;span style="color:#ae81ff"&gt;200&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;headers&amp;#39;&lt;/span&gt;: [
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; (&lt;span style="color:#e6db74"&gt;b&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;content-type&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;b&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;text/plain&amp;#39;&lt;/span&gt;),
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;await&lt;/span&gt; send({
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;type&amp;#39;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#39;http.response.body&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;body&amp;#39;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;b&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;Hello, ASGI World&amp;#39;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="4-definition-of-the-was-web-application-server-layer"&gt;4. Definition of the WAS (Web Application Server) Layer
&lt;/h2&gt;&lt;p&gt;In system architecture, it is important to clearly define the division of roles between the web server (Nginx, Apache) and the WAS (Gunicorn, Uvicorn).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Web Server&lt;/b&gt;: Responsible for serving static files, SSL/TLS termination, reverse proxying, and load balancing.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;WAS&lt;/b&gt;: Responsible for executing business logic, database operations, and generating dynamic content. In the Python ecosystem, WSGI/ASGI servers correspond to this WAS layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="5-architectural-comparison-of-frameworks"&gt;5. Architectural Comparison of Frameworks
&lt;/h2&gt;&lt;h3 id="django"&gt;Django
&lt;/h3&gt;&lt;p&gt;A full-stack framework designed in the WSGI era, encompassing features such as an ORM and an admin panel. Since Django 3.0, native support for ASGI has been added, allowing both synchronous and asynchronous views to coexist.&lt;/p&gt;
&lt;h3 id="fastapi"&gt;FastAPI
&lt;/h3&gt;&lt;p&gt;A modern framework built from the ground up assuming ASGI. It maximizes the use of asynchronous I/O, delivering high throughput particularly in I/O-bound tasks such as inference endpoints for AI/machine learning models. It is also optimized for development efficiency, featuring automatic document generation leveraging type hints.&lt;/p&gt;
&lt;h2 id="findings"&gt;Findings
&lt;/h2&gt;&lt;p&gt;The choice of Python web interface depends on the communication characteristics of the application. For synchronous systems centered on simple CRUD operations, WSGI (Gunicorn + Django/Flask) can ensure sufficient stability. However, when building real-time communication or highly concurrent API servers, transitioning to ASGI (Uvicorn + FastAPI/Django ASGI) is indispensable. In infrastructure design, it is necessary to select the appropriate WAS configuration, considering the impact of these interface standards on resource consumption and latency.&lt;/p&gt;</description></item><item><title>Technical Considerations in FastAPI Architecture Design and Implementation</title><link>https://klifehack.com/en/p/fastapi-architecture-implementation-deep-dive/</link><pubDate>Thu, 28 May 2026 10:04:17 +0900</pubDate><guid>https://klifehack.com/en/p/fastapi-architecture-implementation-deep-dive/</guid><description>&lt;h1 id="technical-analysis-of-fastapi-internal-architecture-and-runtime-behavior"&gt;Technical Analysis of FastAPI Internal Architecture and Runtime Behavior
&lt;/h1&gt;&lt;p&gt;FastAPI is a modern, high-performance web framework for building APIs based on standard Python type hints. This article provides a technical analysis of FastAPI&amp;rsquo;s internal architecture, data validation mechanisms, and asynchronous processing behavior at runtime.&lt;/p&gt;
&lt;h2 id="1-architectural-components-and-design-philosophy"&gt;1. Architectural Components and Design Philosophy
&lt;/h2&gt;&lt;p&gt;FastAPI achieves its functionality by integrating two independent primary libraries.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Starlette:&lt;/b&gt; Manages the foundation of the web ecosystem, including routing, middleware, and compliance with the ASGI specification.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Pydantic:&lt;/b&gt; Handles data validation, serialization, and OpenAPI schema generation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="runtime-control-via-type-hints"&gt;Runtime Control via Type Hints
&lt;/h3&gt;&lt;p&gt;The most significant feature of FastAPI is its utilization of Python type hints not just as static analysis tools, but as runtime logic. The framework references type hints to automate the following processes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;Data Extraction:&lt;/b&gt; Determines where to retrieve values from (Path, Query, Body, or Header).&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Validation:&lt;/b&gt; Applies strict verification rules based on the defined types.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Data Conversion:&lt;/b&gt; Automatically converts strings from URLs into types like &lt;b&gt;int&lt;/b&gt;, &lt;b&gt;float&lt;/b&gt;, or complex Pydantic models.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Documentation Generation:&lt;/b&gt; Reflects accurate data types and constraints in the OpenAPI schema.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example, if a parameter is declared as an &lt;b&gt;int&lt;/b&gt; and conversion fails, FastAPI automatically returns a &lt;b&gt;422 Unprocessable Entity&lt;/b&gt;. This eliminates the need for developers to manually write validation logic.&lt;/p&gt;
&lt;h2 id="2-execution-environment-and-lifecycle-management"&gt;2. Execution Environment and Lifecycle Management
&lt;/h2&gt;&lt;p&gt;FastAPI provides a CLI to control different behaviors between development and production environments.&lt;/p&gt;
&lt;h3 id="differences-in-execution-modes"&gt;Differences in Execution Modes
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Development Mode (fastapi dev):&lt;/b&gt; Auto-reload is enabled, and it binds to 127.0.0.1 by default for security reasons.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Production Mode (fastapi run):&lt;/b&gt; Auto-reload is disabled for stability, and it binds to 0.0.0.0 assuming containerization.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="considerations-in-multi-worker-environments"&gt;Considerations in Multi-worker Environments
&lt;/h3&gt;&lt;p&gt;⚠️ When starting multiple worker processes using the &amp;ndash;workers option, each worker has an independent memory space. Therefore, in-memory global variables (such as lists or counters) are not shared between workers. If state management is required, a design utilizing an external store like Redis or a database is mandatory.&lt;/p&gt;
&lt;h2 id="3-parameter-handling-and-the-annotated-pattern"&gt;3. Parameter Handling and the Annotated Pattern
&lt;/h2&gt;&lt;p&gt;In FastAPI, it is recommended to use &lt;b&gt;typing.Annotated&lt;/b&gt; to separate and integrate type information and metadata.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; typing &lt;span style="color:#f92672"&gt;import&lt;/span&gt; Annotated
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; fastapi &lt;span style="color:#f92672"&gt;import&lt;/span&gt; FastAPI, Query
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;app &lt;span style="color:#f92672"&gt;=&lt;/span&gt; FastAPI()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;@app.get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/items/&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;read_items&lt;/span&gt;(
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; q: Annotated[str &lt;span style="color:#f92672"&gt;|&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;None&lt;/span&gt;, Query(max_length&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;50&lt;/span&gt;)] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;None&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; size: Annotated[int, Query(ge&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;)] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; {&lt;span style="color:#e6db74"&gt;&amp;#34;q&amp;#34;&lt;/span&gt;: q, &lt;span style="color:#e6db74"&gt;&amp;#34;size&amp;#34;&lt;/span&gt;: size}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;💡 By using &lt;b&gt;Annotated&lt;/b&gt;, you can maintain compatibility with standard Python tools while adding framework-specific constraints such as &lt;b&gt;ge=1&lt;/b&gt; (greater than or equal to 1) or &lt;b&gt;max_length&lt;/b&gt;.&lt;/p&gt;
&lt;h2 id="4-data-modeling-with-pydantic"&gt;4. Data Modeling with Pydantic
&lt;/h2&gt;&lt;p&gt;Pydantic models are used for processing request bodies. This allows complex JSON structures to be handled safely as Python objects.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; pydantic &lt;span style="color:#f92672"&gt;import&lt;/span&gt; BaseModel, ConfigDict
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;ItemModel&lt;/span&gt;(BaseModel):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; id: int
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; name: str
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; description: str &lt;span style="color:#f92672"&gt;|&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;None&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;None&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;model_config &lt;span style="color:#f92672"&gt;=&lt;/span&gt; ConfigDict(from_attributes&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;🛠️ When integrating with ORMs (such as SQLAlchemy), setting &lt;b&gt;model_config = ConfigDict(from_attributes=True)&lt;/b&gt; allows data to be read from object attributes in addition to dictionary formats.&lt;/p&gt;
&lt;h2 id="5-asynchronous-execution-model-distinguishing-between-async-def-and-def"&gt;5. Asynchronous Execution Model: Distinguishing between async def and def
&lt;/h2&gt;&lt;p&gt;FastAPI switches the execution thread based on how the function is defined. Understanding this behavior is critical for performance optimization.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;async def:&lt;/b&gt; Executed directly on the event loop. Only non-blocking code (processes involving await) should be written within the function.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;def:&lt;/b&gt; Executed in an external thread pool. This mechanism prevents the event loop from being blocked when synchronous blocking processes (such as time.sleep() or synchronous DB drivers) are included.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;⚠️ &lt;b&gt;Warning:&lt;/b&gt; Calling a blocking function like time.sleep() within an &lt;b&gt;async def&lt;/b&gt; will stop the entire event loop, preventing the server from processing other requests. If blocking processes are necessary, use a standard &lt;b&gt;def&lt;/b&gt; or consider &lt;b&gt;await asyncio.sleep()&lt;/b&gt;.&lt;/p&gt;
&lt;h2 id="6-dependency-injection"&gt;6. Dependency Injection
&lt;/h2&gt;&lt;p&gt;FastAPI&amp;rsquo;s DI system is designed to modularize authentication, database session management, and common parameter processing.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; typing &lt;span style="color:#f92672"&gt;import&lt;/span&gt; Generator
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; fastapi &lt;span style="color:#f92672"&gt;import&lt;/span&gt; Depends
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;get_db_session&lt;/span&gt;() &lt;span style="color:#f92672"&gt;-&amp;amp;&lt;/span&gt;gt; Generator:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; db &lt;span style="color:#f92672"&gt;=&lt;/span&gt; SessionLocal()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;try&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;yield&lt;/span&gt; db
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;finally&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; db&lt;span style="color:#f92672"&gt;.&lt;/span&gt;close()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;💡 In dependencies using &lt;b&gt;yield&lt;/b&gt;, the code up to the &lt;b&gt;yield&lt;/b&gt; is executed before request processing, and the &lt;b&gt;finally&lt;/b&gt; block is executed after the response is sent, ensuring reliable resource cleanup.&lt;/p&gt;
&lt;h2 id="7-middleware-and-cors-configuration"&gt;7. Middleware and CORS Configuration
&lt;/h2&gt;&lt;p&gt;Middleware, which intercepts all requests and responses, plays a vital role in security settings. In particular, CORS configuration to allow access from different domains is essential for integration with frontends.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; fastapi &lt;span style="color:#f92672"&gt;import&lt;/span&gt; FastAPI
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; fastapi.middleware.cors &lt;span style="color:#f92672"&gt;import&lt;/span&gt; CORSMiddleware
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;app &lt;span style="color:#f92672"&gt;=&lt;/span&gt; FastAPI()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;origins &lt;span style="color:#f92672"&gt;=&lt;/span&gt; [
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;http://localhost:3000&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;app&lt;span style="color:#f92672"&gt;.&lt;/span&gt;add_middleware(
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; CORSMiddleware,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; allow_origins&lt;span style="color:#f92672"&gt;=&lt;/span&gt;origins,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; allow_credentials&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; allow_methods&lt;span style="color:#f92672"&gt;=&lt;/span&gt;[&lt;span style="color:#e6db74"&gt;&amp;#34;*&amp;#34;&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; allow_headers&lt;span style="color:#f92672"&gt;=&lt;/span&gt;[&lt;span style="color:#e6db74"&gt;&amp;#34;*&amp;#34;&lt;/span&gt;],
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="8-application-structuring-and-life-events"&gt;8. Application Structuring and Life Events
&lt;/h2&gt;&lt;p&gt;In large-scale applications, &lt;b&gt;APIRouter&lt;/b&gt; is used to split routes and improve maintainability. Additionally, using the &lt;b&gt;lifespan&lt;/b&gt; context manager allows for defining logic that runs only once at application startup and shutdown (such as loading machine learning models or establishing DB connections).&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; contextlib &lt;span style="color:#f92672"&gt;import&lt;/span&gt; asynccontextmanager
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; fastapi &lt;span style="color:#f92672"&gt;import&lt;/span&gt; FastAPI, APIRouter
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;@asynccontextmanager&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;lifespan&lt;/span&gt;(app: FastAPI):
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Startup logic (e.g., connection pool initialization)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;yield&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Shutdown logic (e.g., connection pool cleanup)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;app &lt;span style="color:#f92672"&gt;=&lt;/span&gt; FastAPI(lifespan&lt;span style="color:#f92672"&gt;=&lt;/span&gt;lifespan)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;router &lt;span style="color:#f92672"&gt;=&lt;/span&gt; APIRouter()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;@router.get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;/users&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;get_users&lt;/span&gt;():
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; [{&lt;span style="color:#e6db74"&gt;&amp;#34;username&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;user1&amp;#34;&lt;/span&gt;}]
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;app&lt;span style="color:#f92672"&gt;.&lt;/span&gt;include_router(router)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;FastAPI integrates a robust ASGI foundation via Starlette with strict data validation via Pydantic through the intuitive interface of Python type hints. By understanding the proper use of &lt;b&gt;async def&lt;/b&gt; versus &lt;b&gt;def&lt;/b&gt;, metadata management with &lt;b&gt;Annotated&lt;/b&gt;, and resource control via &lt;b&gt;lifespan&lt;/b&gt;, it is possible to build scalable and highly maintainable API architectures.&lt;/p&gt;</description></item></channel></rss>