pysafecircuit
This Python module provides a CircuitBreaker class that helps in managing failures and timeouts gracefully using the circuit breaker pattern.
Overview
The CircuitBreaker class manages three states:
- CLOSED: Normal operation state where calls are allowed.
- OPEN: Circuit is open due to excessive failures, and calls are blocked.
- HALF_OPEN: After a timeout, the circuit allows a limited number of calls to determine if the underlying service is available.
Dependencies
- threading: For thread synchronization using locks.
- time: For pausing execution between retries.
- datetime and timedelta: For handling timeout calculations.
- typing: For type annotations to specify callback function signatures.
Example Usage
import time
import random
from datetime import datetime, timedelta
from typing import Callable, Optional
from pysafecircuit import CircuitBreaker
def example_function():
# Simulate some operation that may fail
if random.random() < 0.5:
return Exception("an error occurred")
return None
def main():
# Create a new circuit breaker with max_failures=3, timeout=5 seconds, pause_time=1 second, and max_consecutive_successes=2
cb = CircuitBreaker(max_failures=3, timeout=5, pause_time=1, max_consecutive_successes=2)
# Set up the callbacks
cb.set_on_open(lambda: print("Circuit breaker opened!"))
cb.set_on_close(lambda: print("Circuit breaker closed!"))
cb.set_on_half_open(lambda: print("Circuit breaker is half-open, trying again..."))
# Execute the function with circuit breaker protection
for i in range(20):
err = cb.execute(example_function)
if err:
print(f"Attempt {i+1} failed: {err}")
else:
print(f"Attempt {i+1} succeeded")
time.sleep(1) # Simulate some delay between attempts
if __name__ == "__main__":
main()
Features
- State Management: Tracks the state of the circuit (CLOSED, OPEN, HALF_OPEN).
- Callbacks: Allows setting callbacks for when the circuit breaker transitions between states (set_on_open, set_on_close, set_on_half_open).
- Retry Logic: Implements retry logic with configurable parameters (max_failures, timeout, pause_time, max_consecutive_successes).
Metadata
Release files for pysafecircuit 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pysafecircuit-0.1.1.tar.gz | 2.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pysafecircuit-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.9 kB
Release files / pysafecircuit-0.1.1.tar.gz
| Download URL | pysafecircuit-0.1.1.tar.gz |
|---|---|
| Size | 2.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f74706df58ce97818f0d3ad9fa5508cad4bac571d1df8cdcc9eeebe8de7db1d
|
|
BLAKE2b-256 checksum How to use checksums |
9dd679c53c485904e9f1669e6d1dab145d58905534e30be769ff0af4e9ad714d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-18-amd64
|
Release files / pysafecircuit-0.1.1-py3-none-any.whl
| Download URL | pysafecircuit-0.1.1-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cd9835594c20f74a8805d5d80c9ccd3a29ae1c70da375fb6bae39d9f1ee60ade
|
|
BLAKE2b-256 checksum How to use checksums |
4ee4e809f9054165227354182dea5d797ae10105c433e5d62e4a42949b4c492a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.2 Linux/6.1.0-18-amd64
|
Security for pysafecircuit 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.