Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

8664 bites

Page 7

The asyncio Event Loop: One Thread, Many Tasks
Python & FastAPI2 min read

The asyncio Event Loop: One Thread, Many Tasks

The asyncio event loop is a manager for a single-threaded process, juggling tasks to prevent idleness during slow I/O. It's the core of apps like FastAPI, handling network requests efficiently. The footgun is interacting with it directly; use asyncio.run().

Python & FastAPI1 min read

SQLAlchemy connection pooling across Uvicorn workers

Each worker has its own pool; total DB connections equal workers times (pool_size plus max_overflow); overflow connections are temporary; misconfiguration exhausts DB…

How do you protect a FastAPI endpoint using Depends and OAuth2PasswordBearer?
Python & FastAPI2 min read

How do you protect a FastAPI endpoint using Depends and OAuth2PasswordBearer?

OAuth2PasswordBearer sets the token URL, Depends injects it into the endpoint, and FastAPI validates the Bearer header.

What are the three components of a JWT?
Python & FastAPI2 min read

What are the three components of a JWT?

Tests if you know JWT structure beyond library usage. A strong answer lists header, payload, and signature; notes Base64Url encoding; and gives a registered claim like exp. A red flag is confusing signing with encryption.

Coordinating Asyncio Tasks with Locks and Events
Python & FastAPI2 min read

Coordinating Asyncio Tasks with Locks and Events

asyncio sync primitives are traffic signals for coroutines, preventing collisions over shared state. Use a Lock for exclusive access or an Event to signal multiple tasks to proceed. Footgun: these are for asyncio tasks only, not OS threads.

Python & FastAPI2 min read

How should you store user passwords in a database?

Tests knowledge of slow salted hashing versus encryption. Strong answers pick Argon2id or bcrypt, require unique per-user salts, describe verification via re-hashing with constant-time comparison, and cite bcrypt or argon2-cffi.

asyncio Queues: Coordinating Asynchronous Tasks
Python & FastAPI2 min read

asyncio Queues: Coordinating Asynchronous Tasks

An asyncio queue is a channel for coroutines to safely exchange data. It's ideal for producer-consumer patterns, like a web crawler feeding URLs to parsers. The main footgun: it's not thread-safe and must be used within a single event loop.

Implement OAuth2 Password Flow in FastAPI
Python & FastAPI2 min read

Implement OAuth2 Password Flow in FastAPI

Tests FastAPI security integration and stateless auth patterns. A strong answer covers the POST /token endpoint returning a JWT, the OAuth2PasswordBearer dependency, and get_current_user decoding the JWT sub.

Implement RBAC in FastAPI with a JWT role dependency
Python & FastAPI2 min read

Implement RBAC in FastAPI with a JWT role dependency

Build a dependency that decodes the JWT, checks the role, raises 403 if not admin, and inject via Depends.

Python's Asyncio Subprocesses: Non-Blocking Shell Commands
Python & FastAPI2 min read

Python's Asyncio Subprocesses: Non-Blocking Shell Commands

Run external commands without blocking your async app's event loop. asyncio.create_subprocess_shell lets you launch processes and await their results, keeping your server responsive.

How do OAuth2 scopes enable granular permissions in FastAPI versus role-based checks?
Python & FastAPI2 min read

How do OAuth2 scopes enable granular permissions in FastAPI versus role-based checks?

Tests OAuth2 scope granularity vs RBAC and FastAPI SecurityScopes. Strong answers mention JWT claim strings, SecurityScopes per endpoint, and that RBAC is coarse while scopes are fine-grained. Red flag: treating scopes as roles or skipping claim checks.

Async Generators: `yield` in an `async` World
Python & FastAPI2 min read

Async Generators: `yield` in an `async` World

Async generators let you write I/O-bound data streams with the elegance of yield. An async def function with yield produces values one at a time, pausing for I/O without blocking. This is ideal for streaming data from a database.

Python & FastAPI1 min read

Revoking stateless JWTs on logout

A server-side denylist of revoked token IDs checked per request, or short-lived access tokens paired with revocable refresh tokens.

asyncio: Transports Move Bytes, Protocols Decide Which Bytes
Python & FastAPI2 min read

asyncio: Transports Move Bytes, Protocols Decide Which Bytes

asyncio Transports are the "how" (moving bytes), while Protocols are the "what" (deciding which bytes to send). They're the low-level foundation for libraries handling raw socket I/O.

Python & FastAPI2 min read

What JWT claims must you validate beyond the signature?

This tests whether you understand token misuse beyond crypto: time validity, audience and issuer binding, algorithm whitelisting, and required claims enforcement. Red flag: only checking signature and ignoring exp or aud.

asyncio Event Loop Policies: A Deprecated Pattern
Python & FastAPI2 min read

asyncio Event Loop Policies: A Deprecated Pattern

Think of an event loop policy as the global factory for asyncio's event loops, controlling which loop is created and how it's retrieved. It was used to swap implementations, but the entire API is deprecated in Python 3.14 and will be removed in 3.16.

Python & FastAPI2 min read

Frontend on localhost:3000 gets errors calling FastAPI on localhost:8000. Name and fix?

This tests whether different ports mean different origins, causing CORS errors. A strong answer names CORS, notes ports are distinct origins, and outlines using CORSMiddleware with allow_origins.

Debugging Python's Asyncio
Python & FastAPI2 min read

Debugging Python's Asyncio

Debugging asyncio is about finding what's blocking the single-threaded event loop. Use its debug mode to detect slow callbacks and run_in_executor to offload CPU-bound work. The biggest mistake is calling blocking code directly, which stalls the entire app.

Python & FastAPI2 min read

How do you send an email without blocking a FastAPI request?

Tests knowledge of FastAPI's BackgroundTasks for post-response work. Strong answer: import it, inject into the endpoint, define a task function, and call add_task before returning.

SQLAlchemy Engine vs. Session: The Switchboard and the Call
Python & FastAPI2 min read

SQLAlchemy Engine vs. Session: The Switchboard and the Call

Think of SQLAlchemy's Engine as the database switchboard (one per app) and a Session as a single, short-lived phone call (one per request). This pattern is standard in FastAPI for managing database connections.