Registry / http-networking / circuitbreaker

circuitbreaker

JSON →
library2.1.3pypypi✓ verified 26d ago

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 circuitbreaker
INSTALL
IMPORT
SIG · CIRCUITBREAKER
C
circuitbreaker
http-networkingpythonv2.1.3
Install
1.5s avg
Import
202ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.224s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.180s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

circuit
from circuitbreaker import circuit
Commonly used as a decorator for functions (both sync and async) to apply circuit breaker logic.
CircuitBreaker
from circuitbreaker import CircuitBreaker
The class for creating custom circuit breaker instances or for more advanced control.
CircuitBreakerError
from circuitbreaker import CircuitBreakerError
The exception raised when the circuit is open and a protected function is called.

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.

import time from circuitbreaker import circuit # Define a service call that might fail def unreliable_service(): # Simulate failure 75% of the time if time.time() % 4 < 3: raise ConnectionError("Service is currently unavailable!") print("Service call successful!") return "Data from service" # Decorate the unreliable service call with a circuit breaker # Trips after 3 failures, resets after 5 seconds @circuit(fail_max=3, reset_timeout=5, exclude=[ValueError]) def get_data(): return unreliable_service() print("--- Circuit Breaker Quickstart ---") for i in range(10): print(f"Attempt {i+1}:") try: result = get_data() print(f" Result: {result}") except ConnectionError as e: # Handle the underlying service error print(f" Caught service error: {e}") except Exception as e: # Catch CircuitBreakerError or other unexpected errors print(f" Caught general error from circuit: {e}") time.sleep(1)
Debug
Known issues
breakingVersion 2.0.0 completely dropped support for Python 2.x and early Python 3 versions. Applications running on Python 2.x or Python 3.x prior to 3.7 will break upon upgrade.
fix
Upgrade your Python environment to 3.7 or newer before upgrading `circuitbreaker` to version 2.0.0 or later.
affects: >=2.0.0
gotchaBy default, the `@circuit` decorator (and `CircuitBreaker` class without `exceptions` specified) will trip on *any* `Exception`. This can be overly broad; it's often better to specify which exceptions should trip the circuit to avoid tripping on non-recoverable or expected errors.
fix
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.
affects: All
gotchaMisconfiguring `fail_max`, `reset_timeout`, or `recovery_timeout` (for HALF_OPEN state) can lead to a circuit that never opens, opens too frequently, or stays open indefinitely. Understanding the state transitions is crucial.
fix
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.
affects: All
breakingThe `@circuit` decorator no longer accepts `fail_max` (and potentially other circuit configuration parameters like `reset_timeout` or `recovery_timeout`) directly as keyword arguments. This is a breaking API change that results in a `TypeError` if used in code relying on previous behavior.
fix
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.
affects: >=2.0.0
breakingThe `@circuit` decorator does not directly accept parameters such as `fail_max`, `reset_timeout`, or `recovery_timeout`. These parameters are intended for the `CircuitBreaker` class constructor.
fix
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.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'circuitbreaker'
This error occurs when the 'circuitbreaker' package has not been installed in your Python environment or is not accessible on the Python import path.
fix
Install the package using pip: `pip install circuitbreaker`
circuitbreaker.CircuitBreakerError
This exception is raised by the `circuitbreaker` library when the protected function is called while the circuit is in an 'open' state, meaning too many failures have occurred, and the circuit breaker is actively preventing further calls to the unhealthy service.
fix
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.")`
TypeError: ('unnamed CircuitBreaker', 'has no string representation')
This specific `TypeError` occurred in older versions of the `circuitbreaker` library when attempting to convert an unnamed `CircuitBreaker` instance to a string (e.g., via `str(cb)`) due to an internal issue with its string representation.
fix
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')`.
Upgrade
Version history
2.1.3latest on PyPI · released Mar 31, 2025
Audit
Dependencies
PythonrequiredRequires Python 3.7 or newer. Python 2.x is no longer supported since version 2.0.0.
Agent activity
40 hits · last 30 days
node
34
OpenAI (training)
1
Resources
circuitbreaker — pip install circuitbreaker · libregistry