Performance: Path operations (GET, POST, PUT, DELETE)
This affects server response time and client perceived latency when interacting with API endpoints.
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): # Simulate async processing import asyncio await asyncio.sleep(0.1) return {"item_id": item_id} @app.post("/items/") async def create_item(item: dict): # Async and validation handled return item
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") def read_item(item_id: int): # Simulate heavy processing import time time.sleep(2) return {"item_id": item_id} @app.post("/items/") def create_item(item: dict): # No validation or async handling return item
| Pattern | Server Blocking | Concurrent Requests | Response Time | Verdict |
|---|---|---|---|---|
| Synchronous blocking code | High (blocks event loop) | Low (serial processing) | Slow (seconds delay) | [X] Bad |
| Asynchronous non-blocking code | Low (event loop free) | High (parallel processing) | Fast (milliseconds delay) | [OK] Good |
from fastapi import FastAPI
app = FastAPI()
items = {5: "old value"}
@app.put('/items/{item_id}')
async def update_item(item_id: int):
items[item_id] = "new value"
return items[item_id]from fastapi import FastAPI
app = FastAPI()
items = {1: "apple", 2: "banana"}
@app.delete('/items/{item_id}')
async def delete_item(item_id: int):
del items[item_id]
return {"message": "Item deleted"}Pass the test and this pattern moves unknown