24 articles
Knowledge Base
Python
24 articles in the Python category.
- 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.
- Auditing Python dependencies with pip-audit and Safety Scan a Python project for known vulnerabilities with pip-audit (PyPA) and Safety, integrate the check into CI, and triage findings without breaking deploys.
- 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.
- Pydantic v2 patterns: settings, validators, and discriminated unions Apply Pydantic v2 in production: BaseSettings for env config, field validators, model_validator, discriminated unions, and the Rust-backed performance shift.
- 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 stdlib venv, and pipenv for isolating Python environments — what they do, what they don't, and the operational reasons to pick one.
- 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.
- Installing Python 3 and managing venvs on Ubuntu/Debian Pick the right Python on Ubuntu, install pip and venv, and structure per-project virtual environments so a system update never breaks a running service.
- Deploying ASGI apps under Hypercorn in production Run an ASGI app under Hypercorn with HTTP/2, HTTP/3 (QUIC), and systemd socket activation, with worker tuning and Nginx termination for production.