This guide walks you through protecting your first integration point with PyBreaker. By the end you’ll have a working circuit breaker that opens on repeated failures, short-circuits calls while open, and automatically attempts recovery after a timeout — all in a handful of lines of Python.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/danielfm/pybreaker/llms.txt
Use this file to discover all available pages before exploring further.
Create a CircuitBreaker
Instantiate a
CircuitBreaker at application scope — as a module-level global or injected singleton. Do not create a new instance per request; the circuit breaker must persist across calls to accumulate failure counts and track state.fail_max— number of consecutive failures required to open the circuit.reset_timeout— seconds to wait in the open state before allowing a trial call (half-open).
Protect a Function with the Decorator
Apply the circuit breaker as a decorator. The wrapped function behaves exactly as before when the circuit is closed — the breaker is completely transparent during normal operation.
Handle Circuit Open Errors
When the circuit opens (after
fail_max consecutive failures), any call to the protected function immediately raises CircuitBreakerError — without executing the underlying function. Catch this exception separately from real errors so you can serve a fallback.Alternative Usage Patterns
The decorator isn’t the only way to use PyBreaker. Two additional patterns give you more control when you can’t or don’t want to decorate a function. Direct call — invoke any callable through the breaker without modifying it:call(), and context manager — enforce the same circuit breaker logic and update the same state.
Want to understand exactly how the closed → open → half-open transitions work, how
success_threshold affects recovery, and how excluded exceptions let you filter out business errors? See the Core Concepts section for a deep dive into circuit breaker states and behavior.