Skip to main content

A Python library implementing a circuit breaker pattern

Reason this release was yanked:

Info Outdated

Project description

CircuitBreaker

This Python library provides a circuit_breaker decorator for implementing the circuit breaker pattern in your applications.

The circuit breaker pattern is a resiliency mechanism that helps protect external dependencies from cascading failures. It works by monitoring the success and failure rates of calls to a function and taking preventive measures to avoid overloading the dependency.

How it Works

The circuit_breaker decorator takes several optional arguments:

  • failure_threshold: The number of consecutive failures allowed before the circuit opens (defaults to 3).
  • recovery_timeout: The time window in seconds after which the circuit attempts recovery (defaults to 10).
  • replace_function: A function to execute during circuit open state (optional).
  • consecutive: A flag indicating whether consecutive failures trigger opening (defaults to False).

Here's a basic example of using the decorator:

from cirbreak import circuit_breaker

@circuit_breaker(failure_threshold=5)
def external_call():
    # Simulate a call to an external service
    if random.random() > 0.5:
        raise Exception("External service error")
    return "Success!"

try:
    response = external_call()
    print(response)
except Exception as e:
    print(f"Error: {e}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cirbreak-0.1.0.tar.gz (4.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page