A Python implementation of the Circuit Breaker pattern, designed to prevent applications from repeatedly trying to perform an operation that is likely to fail. It supports both synchronous and asynchronous functions and has been classified as a 'Critical Project' on PyPI. The current version is 2.1.3.
pip install circuitbreakerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `@circuit` decorator to protect a potentially unreliable synchronous function. It configures the circuit to trip after 3 failures (`fail_max=3`) and attempt to reset after 5 seconds (`reset_timeout=5`). It also shows how to exclude certain exceptions from tripping the circuit and handling both the underlying service errors and `CircuitBreakerError` when the circuit is open.
Upgrade your Python environment to 3.7 or newer before upgrading `circuitbreaker` to version 2.0.0 or later.
Use the `exceptions` parameter in the `@circuit` decorator or `CircuitBreaker` constructor (e.g., `@circuit(exceptions=ConnectionError)`) to specify the types of exceptions that should cause the circuit to trip. Alternatively, use `exclude` for exceptions that should *not* trip the circuit.
Carefully review the documentation for `fail_max` (number of failures before opening), `reset_timeout` (time in seconds to wait before transitioning to HALF_OPEN), and `recovery_timeout` (time for HALF_OPEN to attempt recovery). Adjust these parameters based on the expected behavior and recovery time of your external service.
Consult the `circuitbreaker` library's documentation for the specific version being used. Configuration parameters like `fail_max` may now need to be passed to the `CircuitBreaker` class constructor directly, or the decorator's API might have been refactored to use a different configuration mechanism.
To configure parameters like `fail_max`, `reset_timeout`, or `recovery_timeout`, first instantiate `CircuitBreaker` with these arguments (e.g., `my_breaker = CircuitBreaker(fail_max=3, reset_timeout=5)`) and then use the `my_breaker` instance as a decorator (e.g., `@my_breaker`). Alternatively, review the documentation for parameters directly supported by the `@circuit` decorator.
Install the package using pip: `pip install circuitbreaker`
Catch `circuitbreaker.CircuitBreakerError` in your code to handle situations where the circuit is open, allowing you to implement fallback logic or inform the user gracefully. Example: `from circuitbreaker import circuit, CircuitBreakerError
@circuit
def unreliable_service_call():
raise ConnectionError("Service unavailable")
try:
unreliable_service_call()
except CircuitBreakerError:
print("Service is currently unavailable, circuit is open.")`Upgrade to the latest version of the `circuitbreaker` library using `pip install --upgrade circuitbreaker`. Alternatively, ensure your `CircuitBreaker` instances are named by passing the `name` parameter during instantiation, e.g., `cb = CircuitBreaker(name='my_breaker')`.