Performance: ASGI and async-first architecture
This affects how quickly the server can handle multiple requests and respond without blocking, improving interaction speed and throughput.
from fastapi import FastAPI import asyncio app = FastAPI() @app.get("/async") async def async_endpoint(): await asyncio.sleep(2) # non-blocking sleep return {"message": "done"}
from fastapi import FastAPI app = FastAPI() @app.get("/sync") def sync_endpoint(): import time time.sleep(2) # blocking sleep return {"message": "done"}
| Pattern | Concurrency | Event Loop Blocking | Input Delay | Verdict |
|---|---|---|---|---|
| Synchronous blocking calls | Low (serial) | High (blocks event loop) | High (long input delay) | [X] Bad |
| Async-first non-blocking calls | High (concurrent) | Low (event loop free) | Low (fast input response) | [OK] Good |
async def followed by the function name.async def. Options C and D have invalid syntax. def read_root(): return {"message": "Hello"} is synchronous.from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/wait")
async def wait_and_return():
await asyncio.sleep(1)
return {"status": "done"}await asyncio.sleep(1), which pauses the function asynchronously for 1 second.from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/error")
def error_route():
await asyncio.sleep(1)
return {"message": "done"}await but is defined without async, which is invalid syntax.await, the function must be declared with async def.asyncio.gather runs multiple async tasks at the same time, improving speed.Pass the test and this pattern moves unknown