Skip to main content

Appendix-02-ASGIApplication_In_FastAPI

Overview diagram

Key components

Uvicorn Server: Acts as the high-performance ASGI (Asynchronous Server Gateway Interface) server that handles low-level network communication (receiving requests and sending responses). It runs an asynchronous event loop, typically using asyncio.get_running_loop.

FastAPI app: The Python application framework that defines the API logic.

H11 Protocol: The specific implementation (default in Uvicorn) for handling the HTTP/1.1 protocol.

Router/Routes: Mechanisms within FastAPI (built on Starlette) that map the incoming request URL and HTTP method (e.g., @app.get, @app.post) to the appropriate function (path operation function)

Request Flow Overview

  1. Incoming Request: A request arrives at the Uvicorn server's listening host and port

  2. Protocol Handling: The Uvicorn server, via the H11 protocol implementation, processes the raw incoming HTTP request data.

  3. ASGI Interface: The server communicates with the FastAPI application using the ASGI standard. It calls the application's call method, passing specific scopes, receive, and send channels to manage the request's lifecycle asynchronously.

  4. Routing: FastAPI's internal router inspects the request details (like URL path and HTTP method) and matches it to a defined route within the application's registered routes list.

  5. Path Operation Function: Once a match is found, the associated path operation function in the FastAPI application is executed to process the request logic and generate a response.