/hello?from fastapi import FastAPI app = FastAPI() @app.get('/hello') async def hello(): return {'message': 'Hello, async world!'}
The async endpoint returns a Python dictionary. FastAPI converts this dict to a JSON response automatically. So the client receives a JSON object with the key 'message' and the value 'Hello, async world!'.
/wait?
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get('/wait')
async def wait():
await asyncio.sleep(2)
return {'status': 'done'}Using await asyncio.sleep(2) pauses this endpoint without blocking the server. Other requests can be handled during this wait time because FastAPI uses async-first architecture.
from fastapi import FastAPI app = FastAPI()
Option A defines a normal function (def) but uses 'await' inside it, which is invalid syntax. 'await' must be inside an async function.
/error raise a RuntimeError?
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get('/error')
def error():
asyncio.create_task(asyncio.sleep(1))
return {'status': 'started'}asyncio.create_task requires an active event loop, which is only available inside async functions. Calling it in a normal def function causes a RuntimeError.
ASGI supports asynchronous concurrency, enabling FastAPI to handle many requests at once without waiting for slow operations. WSGI is synchronous and blocks during long tasks.