Python Examples

Page 2 of 3

pythonSnippet

Token Bucket Rate Limiter

Implement a token bucket that refills at a steady rate and allows short bursts, a standard algorithm for shaping request traffic.

pythonSnippet

Redis Sentinel Failover Client

Connect through Redis Sentinel for automatic failover, detecting role changes, retrying reads on replicas, and fencing split-brain writes.

pythonSnippet

Retry Decorator With Backoff

Decorate a function so transient exceptions are retried a fixed number of times with growing delay, keeping call sites free of retry plumbing.

pythonSnippet

Rich Table Live Display CLI

Render dynamic CLI tables with Rich Live mode, updating rows in place as background tasks progress and finishing with a clean summary.

pythonSnippet

Typer Async Command Options

Build async CLI commands with Typer, combining type-annotated options, enum choices, environment fallbacks, and per-command validation.

pythonSnippet

Asyncio Queue Worker Pool

Fan out jobs to a bounded asyncio queue, run a fixed worker pool, and shut down cleanly with sentinel values and task_done.

pythonSnippet

Injectable Clock For Tests

Abstract time behind a Clock protocol, provide a fake clock that can advance instantly, and drive a TTL cache without sleeping.

pythonSnippet

Thread Pool Map With Timeout

Map a function over items with a bounded thread pool, preserve input order, and cancel pending work when the overall deadline expires.