26 articles
Knowledge Base
Python
26 articles in the Python category · page 1 of 2.
- Pydantic v2 patterns: settings, validators, and discriminated unions Set up Pydantic v2 in a production Python service, from BaseSettings for environment config to field validators, model validators and discriminated unions. You will also see what the Rust-backed core changes when you migrate from v1.
- pip-audit vs Safety: Python dependency scanning as a CI gate What pip-audit actually scans, how it differs from Safety on data source and licensing, and how to run it as a CI gate that fails honestly: pinned tool version, environment vs lockfile, --fix in CI, and suppressions with an expiry.
- Migrating a Python project to uv: the uv.lock workflow Adopt the uv lockfile workflow on a real project — import an existing requirements.txt or Poetry set, commit uv.lock, and reproduce the same resolved versions with uv sync --frozen on a laptop, in CI, and in a Docker image.
- How to Deploy Gunicorn with systemd Behind Nginx A gunicorn systemd setup that runs a Flask or Django app in production: a Unix socket, the right worker count, an Nginx reverse proxy, and graceful reloads that drop no requests.
- Deploying Python apps with Gunicorn and systemd Run a Django or Flask app under Gunicorn with sync or gthread workers, expose it on a UNIX socket, and put systemd in charge of the lifecycle.
- uWSGI behind Nginx for Python applications Configure uWSGI for a Python WSGI app with the binary uwsgi protocol, an Emperor-managed vassal, and Nginx as the front door.
- Debugging Django in production without DEBUG=True Reproduce and diagnose Django bugs in production safely — without DEBUG=True — using structured logs, Sentry traces, shell_plus, query inspection, and SQL EXPLAIN.
- Pinning Python dependencies with pip-tools and hashes Use pip-tools to compile a hash-pinned requirements.txt from a high-level requirements.in, with operational rules for updates, security patches, and CI sync.
- Structured logging in Python with Loguru and stdlib alternatives Emit structured JSON logs from a Python service with Loguru, plus the stdlib python-json-logger path — including journald, log rotation, and Sentry forwarding.
- Scheduling periodic Celery tasks with Beat and django-celery-beat Run Celery Beat as a separate systemd service, store the schedule in the database for live edits, and prevent duplicate dispatches in a multi-host deployment.
- Running Sanic in production with multi-worker and Nginx Deploy a Sanic application in production: multi-worker fast mode, systemd unit, Nginx termination, and the operational quirks specific to Sanic's built-in server.
- Running Celery with Redis as broker and result backend Set up Celery 5.x with Redis as broker and result backend, supervised by systemd, with prefetch, acks_late, and visibility timeout tuned for production.
- cProfile and flamegraph generation for Python benchmarks Profile a Python script or benchmark with cProfile, dump the .prof file, and convert it to a flamegraph SVG with flameprof or snakeviz for deterministic call analysis.
- Managing Python dependencies with pip and requirements files Use requirements.txt, constraints files, and pip-tools to get reproducible Python installs across CI and production hosts.
- asyncio patterns that survive production The asyncio constructs and rules of thumb that hold up under real load — task groups, cancellation, timeouts, bounded concurrency, and the patterns that blow up at 3 a.m.
- Picking a Python project manager: Poetry, PDM, or uv Compare Poetry, PDM, and uv for production Python projects — lockfile shape, resolver speed, PEP 621 adherence, and the CI footprint of each tool.
- Python multiprocessing vs threading: choosing for the GIL Pick between threading, multiprocessing, asyncio, and ProcessPoolExecutor for a Python workload — the GIL, fork vs spawn, shared state, and operational cost.
- Profiling Python in production with py-spy Attach py-spy to a running Python process in production to capture a flamegraph or live thread dump without restarting the app or instrumenting the code.
- Deploying FastAPI under Uvicorn workers in production Run an ASGI FastAPI app under Uvicorn or Gunicorn-managed Uvicorn workers, terminated by Nginx, with the worker count and lifespan hooks tuned for production.
- Deploying Django Channels for WebSocket production traffic Run a Django Channels app under Daphne with Redis as the channel layer, supervised by systemd, with Nginx WebSocket upgrade and load-balanced workers.
- virtualenv vs venv vs pipenv: which to use, and why Compare virtualenv, the built-in venv module, and pipenv so you can pick one Python virtual environment tool with confidence. You will see how each one works, where pipenv falls short, and how to pair venv with a lockfile tool for repeatable installs.
- Running mypy in strict mode in production codebases Adopt mypy --strict on a Python project — gradual enablement, third-party stubs, monorepo config, and CI gating that doesn't block hot fixes.
- Django: Gunicorn vs Daphne, when to switch ASGI Decide between Gunicorn-WSGI and Daphne-ASGI for a Django deployment based on WebSocket needs, async views, long-poll routes, and operational tradeoffs.
- Wiring Black and Ruff into a Python formatting pipeline Configure Black and Ruff together for a Python project — formatter and linter responsibilities split cleanly, with pre-commit hooks and CI gate.