tezvyn:

Python & FastAPI

Python, Django, FastAPI, Flask, async Python

245 bites

Python & FastAPI84 sec read

asyncio.gather vs asyncio.wait

WHAT IT TESTS: Whether you know how each aggregates results and handles errors. OUTLINE: gather returns ordered results and propagates the first exception (or captures them); wait returns done/pending sets and never raises, you inspect each.

Python & FastAPI85 sec read

How FastAPI uses type hints for validation

WHAT IT TESTS: Whether you know FastAPI leans on Pydantic and type hints. OUTLINE: Hints drive parsing, validation, and conversion; a path declared int is coerced or returns 422; OpenAPI is auto-generated.

Python & FastAPI2 min read

Explain multi-stage Docker builds for Python and builder vs runtime

Tests separation of build-time and runtime concerns. A strong answer contrasts the builder stage (gcc, headers, wheels) with the runtime stage (slim base, copied artifacts, no compiler). Red flag: citing size alone while ignoring security and caching.

Python & FastAPI2 min read

How do you manage configuration and secrets for a containerized FastAPI app?

Tests 12-factor config separation and Docker secret hygiene. A strong answer uses pydantic-settings with runtime env vars, lru_cache, and keeps .env out of the image. Red flag: baking credentials into Dockerfile layers or committing .env files.

What are the key responsibilities for Nginx versus Uvicorn?
Python & FastAPI2 min read

What are the key responsibilities for Nginx versus Uvicorn?

This tests the reverse-proxy versus ASGI-server boundary. A strong answer gives Nginx TLS termination, static files, buffering, and load balancing, while Uvicorn runs the Python app and async workers.

Python & FastAPI2 min read

Why use an ASGI server like Uvicorn instead of the dev server?

Tests: dev vs prod environment distinction. Answer: Uvicorn is a prod server program; contrast dev server's constant restart/break/fix cycle with prod needs for performance, stability, and uninterrupted access.

Walk me through a basic Dockerfile for a FastAPI app
Python & FastAPI2 min read

Walk me through a basic Dockerfile for a FastAPI app

WHAT IT TESTS: Docker layering and build cache for Python containers. ANSWER OUTLINE: Slim base, install deps before app code to cache layers, expose port, exec-form CMD for Uvicorn. RED FLAG: Shell-form CMD or code-before-requirements, killing cache.

How do you inspect WebSocket close codes in FastAPI?
Python & FastAPI2 min read

How do you inspect WebSocket close codes in FastAPI?

Tests WebSocket lifecycle handling in FastAPI. Strong answers catch the disconnect exception, read its code attribute, and log 1000 for normal closures versus 1001/1006 for crashes.

Python & FastAPI2 min read

How do you handle slow startup without blocking the FastAPI event loop?

It tests FastAPI lifespan events and event loop hygiene. Use an async lifespan to offload blocking model loading to a thread pool, track readiness with a global flag, and return 503 for early requests. Never block the event loop in startup handlers.

How do you broadcast WebSocket messages to all clients across server nodes?
Python & FastAPI2 min read

How do you broadcast WebSocket messages to all clients across server nodes?

Tests WebSocket horizontal scaling and pub/sub backplanes. A strong answer names a broker like Redis, describes cross-node fan-out, and keeps connection state purely local.

How do you authenticate a FastAPI WebSocket connection?
Python & FastAPI2 min read

How do you authenticate a FastAPI WebSocket connection?

This tests WebSocket limits and FastAPI dependency injection. Pass the JWT via query parameter or cookie at handshake, validate it with Depends, and reject with HTTP 403 or 1008 close.

Python & FastAPI2 min read

Use startup events to initialize a database pool and inject it

This tests FastAPI lifespan hooks and dependency injection for shared state. A strong answer creates the pool in an async startup handler, stores it on app.state, and accesses it via a dependency in routes. A red flag is creating a fresh pool per request.

How do you handle a WebSocket client disconnect in FastAPI?
Python & FastAPI2 min read

How do you handle a WebSocket client disconnect in FastAPI?

WHAT IT TESTS: know FastAPI needs explicit cleanup for multiple WebSocket clients. ANSWER OUTLINE: cite installing websockets, Handling disconnections and multiple clients pattern, and Depends. RED FLAG: saying FastAPI auto-cleans dead sockets with no cleanup.

Python & FastAPI2 min read

What are startup and shutdown events in FastAPI?

Tests app lifespan hooks and resource lifecycle. Startup creates DB pools before traffic arrives; shutdown closes them after the last request. These decorators are deprecated; prefer lifespan context managers. Red flag: per-request middleware.

How do you define a WebSocket endpoint in FastAPI?
Python & FastAPI2 min read

How do you define a WebSocket endpoint in FastAPI?

WHAT IT TESTS: async endpoint wiring and the accept-receive-send lifecycle. ANSWER OUTLINE: import WebSocket, use @app.websocket, await accept, receive_text, then send_text. RED FLAG: forgetting accept or treating it like a standard HTTP route.

Python & FastAPI2 min read

How do you directly modify FastAPI's generated OpenAPI dictionary?

Tests deep FastAPI lifecycle knowledge. Override app.openapi: save the original, call it to get the dict, mutate it, cache on app.openapi_schema, and return. Red flag: rewriting /openapi.json in middleware or touching the schema cache directly.

How do you disable FastAPI docs but keep the OpenAPI schema?
Python & FastAPI2 min read

How do you disable FastAPI docs but keep the OpenAPI schema?

Tests FastAPI constructor routing: docs_url, redoc_url, and openapi_url. Answer: pass docs_url=None and redoc_url=None while keeping openapi_url="/openapi.json", gated by env var. Red flag: middleware or manual route deletion instead of native configuration.

Add a custom x- field to a FastAPI path operation schema
Python & FastAPI2 min read

Add a custom x- field to a FastAPI path operation schema

Tests knowledge of FastAPI's built-in OpenAPI extension hook. Answer: cite the path operation decorator's extra-schema dict (OpenAPI Extra) to merge x-internal-id directly. Red flag: proposing manual JSON editing or schema post-processing.

How do you mark a FastAPI endpoint as deprecated?
Python & FastAPI2 min read

How do you mark a FastAPI endpoint as deprecated?

This tests decorator-level OpenAPI configuration in FastAPI. Pass deprecated=True to the path operation decorator, e.g. @app.get("/old", deprecated=True), so Swagger UI shows a strikethrough. Red flag: burying a deprecation warning in the docstring instead.

How do you group FastAPI endpoints under tags and describe groups?
Python & FastAPI2 min read

How do you group FastAPI endpoints under tags and describe groups?

Tests FastAPI's two-step tag system: decorators label routes, and openapi_tags supplies group descriptions. Good answers cover tagging paths with tags=["users"], then defining metadata in FastAPI(openapi_tags=[...]) with matching names.