Skip to content

FastAPI Internals


The Speaker

bg:60% 70%

Marcelo Trylesinski


OSS Maintainer

### Uvicorn ![w:450](https://raw.githubusercontent.com/tomchristie/uvicorn/master/docs/uvicorn.png)
### Starlette ![w:600](https://raw.githubusercontent.com/koddr/starlette-logo/master/src/dark/svg/starlette__full_logo__with_text__dark.svg)

What is FastAPI?

w:750


How do I run FastAPI?

uvicorn main:app or...

www.uvicorn.org


How do I run FastAPI?

hypercorn main:app or...

github.com/pgjones/hypercorn


How do I run FastAPI?

granian main:app

github.com/emmett-framework/granian


How the interactions happen?

w:1000


Let's focus on...

w:900


But... How it really happens?

w:800


ASGI application

Scope = Dict[str, Any]
Receive = Callable[[], Awaitable[Dict[str, Any]]]
Send = Callable[[Dict[str, Any]], Awaitable[None]]

async def app(scope: Scope, receive: Receive, send: Send):
    ...

Simplest ASGI application

async def app(scope, receive, send):
    body = b"Hello world!"
    headers = [(b"content-type", b"text/plain"), (b"content-length", str(len(body)).encode())]

    await send({"type": "http.response.start", "status": 200, "headers": headers})
    await send({"type": "http.response.body", "body": body})

After the connection is established...

w:1000


If the client sends a body...

w:800


Then... It's the application's turn!

w:1000


Middleware Stack

w:1000


Middleware Stack

from fastapi import FastAPI
from src.middleware import CustomMiddleware, AnotherCustomMiddleware

app = FastAPI()
app.add_middleware(CustomMiddleware)
app.add_middleware(AnotherCustomMiddleware)

Middleware Stack

w:900


Routing

w:1000


Routing

w:650


Dependency Injection

w:800


Dependency Injection

w:1100


Dependency Injection

### Async
async def dependency():
    return "Hello, World!"
### Sync
def dependency():
    return "Hello, World!"
--- ## Input/Output Validation
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Input(BaseModel):
    name: str
    age: int

class Output(Input):
    id: int

@app.post("/")
async def read_item(input: Input) -> Output:
    return await database_insert(input)
--- ## Input/Output Validation ![w:900](assets/validation.png) --- ## WebSockets ![w:800](assets/websocket-fastapi.png) --- ## WebSockets ![w:800](assets/websocket-data.png) --- ## Async vs Sync When should I use... --- ## Bonus: FastAPI Tips https://github.com/Kludex/fastapi-tips --- ## Try Logfire! 🚀 ![w:800](assets/logfire.png) --- # Thank You! [marcelotryle.com](https://www.marcelotryle.com) Marcelo Trylesinski @marcelotryle Kludex