Performance: First FastAPI application
This affects the server response time and initial page load speed for API-driven applications.
from fastapi import FastAPI app = FastAPI() @app.get("/data") async def get_data(): return {"message": "Hello World"}
from fastapi import FastAPI import time app = FastAPI() @app.get("/data") def get_data(): time.sleep(3) # Simulate slow blocking operation return {"message": "Hello World"}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Blocking synchronous endpoint | N/A | N/A | Delays initial paint | [X] Bad |
| Async non-blocking endpoint | N/A | N/A | Enables fast initial paint | [OK] Good |
@app.get("/") decorator do in a FastAPI application?@app.get("/") decorator tells FastAPI to listen for GET requests at the root URL path.FastAPI().FastAPI(). app = fastapi() uses lowercase which is incorrect. app = FastAPI misses parentheses, so it is a class reference, not an instance. app = FastAPI.create() uses a non-existent method./hello?
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
def say_hello():
return {"message": "Hello, FastAPI!"}/hello is defined with a GET method. The function returns a dictionary with key "message" and value "Hello, FastAPI!".from fastapi import FastAPI
app = FastAPI()
@app.get("/items")
def read_items():
return {"items": [1, 2, 3]}return is not indented, causing a syntax error./greet/Alice. Which code correctly implements this?{name} in the path to capture the user name from the URL.name: str and return a dictionary with a greeting using an f-string to insert the name.Pass the test and this pattern moves unknown