Skip to content

Commit 0fd1b93

Browse files
Asgi documentation (#17)
* Add devcontainer configuration for development setup * Create setup.sh * Add Codespaces prebuild workflow configuration * Remove unnecessary cache configuration from CI workflow * Config changes (#14) * Codespaces prebuild configs * Add devcontainer configuration for development setup * Create setup.sh * Add Codespaces prebuild workflow configuration * Remove unnecessary cache configuration from CI workflow * Codespaces (#13) * Add devcontainer configuration for development setup * Create setup.sh * Add Codespaces prebuild workflow configuration * Remove unnecessary cache configuration from CI workflow * Delete style.css * Add ASGI and Execution Model documentation for FastAPI
1 parent 9283994 commit 0fd1b93

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# ASGI and Execution Model in FastAPI
2+
3+
## 1. Introduction
4+
5+
Modern backend systems require the ability to handle a large number of concurrent requests efficiently. Traditional synchronous models often become bottlenecks under high I/O workloads. To address this, asynchronous execution models have become a standard approach in modern web frameworks.
6+
7+
FastAPI is built on top of the ASGI (Asynchronous Server Gateway Interface) specification, which enables non-blocking request handling and high concurrency.
8+
9+
---
10+
11+
## 2. Problem Definition
12+
13+
Traditional Python web applications relied on WSGI (Web Server Gateway Interface), which follows a synchronous execution model.
14+
15+
Limitations of WSGI:
16+
17+
- Blocking request handling
18+
- Poor performance under I/O-bound workloads
19+
- Limited scalability for real-time systems
20+
21+
This creates challenges when building:
22+
23+
- AI inference APIs
24+
- agent-based systems
25+
- streaming or real-time applications
26+
27+
---
28+
29+
## 3. Core Concept: ASGI
30+
31+
ASGI is a specification that defines how web servers communicate with Python applications asynchronously.
32+
33+
An ASGI application operates using three core components:
34+
35+
- scope → metadata about the request (type, headers, path)
36+
- receive → channel to receive incoming data
37+
- send → channel to send responses
38+
39+
---
40+
41+
## 4. ASGI Execution Flow
42+
43+
sequenceDiagram
44+
participant Client
45+
participant Uvicorn (ASGI Server)
46+
participant FastAPI App
47+
48+
Client->>Uvicorn (ASGI Server): HTTP Request
49+
Uvicorn (ASGI Server)->>FastAPI App: scope
50+
FastAPI App->>Uvicorn (ASGI Server): receive()
51+
FastAPI App->>Uvicorn (ASGI Server): send(response)
52+
Uvicorn (ASGI Server)->>Client: HTTP Response
53+
54+
## Figure: Interaction between client, ASGI server, and FastAPI application.
55+
56+
---
57+
58+
## 5. FastAPI System Perspective
59+
60+
FastAPI does not operate in isolation. It is part of a layered architecture:
61+
62+
flowchart LR
63+
A[Client] --> B[Uvicorn ASGI Server]
64+
B --> C[FastAPI Application]
65+
C --> D[Starlette (Routing & Middleware)]
66+
C --> E[Pydantic (Validation)]
67+
68+
Components:
69+
70+
- Uvicorn → ASGI server handling network communication
71+
- FastAPI → API layer defining endpoints
72+
- Starlette → routing, middleware, background tasks
73+
- Pydantic → data validation and parsing
74+
75+
---
76+
77+
## 6. Request Handling Model
78+
79+
FastAPI leverages Python’s "async" / "await" syntax:
80+
81+
from fastapi import FastAPI
82+
83+
app = FastAPI()
84+
85+
@app.get("/")
86+
async def root():
87+
return {"message": "Hello World"}
88+
89+
Key Idea:
90+
91+
- The function is non-blocking
92+
- While waiting (e.g., DB call), other requests can be processed
93+
94+
---
95+
96+
## 7. Trade-offs and Limitations
97+
98+
While ASGI provides significant advantages, it introduces complexity:
99+
100+
Advantages:
101+
102+
- High concurrency
103+
- Efficient I/O handling
104+
- Suitable for AI/ML inference APIs
105+
106+
Limitations:
107+
108+
- Increased conceptual complexity
109+
- Debugging async code is harder
110+
- Not always beneficial for CPU-bound tasks
111+
112+
---
113+
114+
## 8. Key Takeaways
115+
116+
- FastAPI is built on the ASGI specification
117+
- ASGI enables asynchronous, non-blocking execution
118+
- The execution model revolves around:
119+
- "scope", "receive", "send"
120+
- FastAPI acts as a system boundary layer between clients and backend logic
121+
122+
---
123+
124+
## 9. Next
125+
126+
→ Request-Response Lifecycle in FastAPI

0 commit comments

Comments
 (0)