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.

8667 bites

Page 280

Python & FastAPI2 min read

SQLAlchemy: Control When Your Relationships Load

SQLAlchemy's default lazy loading is convenient but can cause an N+1 query storm. Use eager loading (joinedload, selectinload) for collections you'll access to prevent many database round trips.

Python & FastAPI2 min read

Beanie: Python Objects as MongoDB Documents

Beanie maps Pydantic models to MongoDB documents, letting you interact with the database using Python objects instead of raw queries. Use it in async apps like FastAPI for rapid, type-safe CRUD.

Motor: Don't Block Your Python App on MongoDB
Python & FastAPI2 min read

Motor: Don't Block Your Python App on MongoDB

Motor is the async bridge for Python apps to talk to MongoDB without blocking. Use it in FastAPI or other async frameworks to keep your server responsive during database queries.

Python & FastAPI2 min read

Alembic: Version Control for Your Database Schema

Alembic is like Git for your database schema, providing versioned, reversible changes. Use it with SQLAlchemy to evolve your database structure alongside your code. The footgun is that autogeneration can miss changes; always review generated scripts.

Python & FastAPI2 min read

SQLAlchemy 2.0: Async Without Blocking the Event Loop

SQLAlchemy 2.0 wraps its synchronous core with an async API, letting you await database calls without blocking your app's event loop. Use it in frameworks like FastAPI.

Python & FastAPI2 min read

SQLAlchemy Declarative: Python Classes as Database Tables

SQLAlchemy's Declarative Mapping lets you define database tables as Python classes. You write a class with typed attributes, and SQLAlchemy generates the SQL. It's the standard way to use the ORM, turning database rows into Python objects.

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.

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.

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.

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.

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'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.

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.

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.

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().

FastAPI: Mounting Independent Sub-Applications
Python & FastAPI2 min read

FastAPI: Mounting Independent Sub-Applications

Mounting delegates a URL prefix to a separate FastAPI app, giving it its own isolated logic and API docs. Use it to combine microservices or isolate domains. The footgun: the main app's dependencies and middleware do not apply to the sub-app.

Python & FastAPI2 min read

FastAPI: Managing Environment-Specific Settings

Treat app configuration like a contract, not hardcoded values. Pydantic Settings defines required variables (like API keys) and loads them from the environment, preventing you from shipping dev settings to production.

Pydantic BaseSettings: Typed, Layered Configuration
Python & FastAPI2 min read

Pydantic BaseSettings: Typed, Layered Configuration

Pydantic's BaseSettings treats configuration as typed data, not just strings. It automatically loads and validates settings from environment variables, .env files, and secrets stores into a Python object.

FastAPI: Splitting Your App with `include_router`
Python & FastAPI1 min read

FastAPI: Splitting Your App with `include_router`

app.include_router is like plugging a pre-wired power strip of API endpoints into your main FastAPI app. It lets you organize a large app into smaller files by feature, then combine them. The footgun is forgetting to add a URL prefix for each router.

Python & FastAPI2 min read

FastAPI's APIRouter: Grouping Routes into Modules

Think of APIRouter as a mini-FastAPI app for organizing endpoints. It lets you group related paths, like all user routes, into a separate file. This is crucial for keeping large applications maintainable.