Skip to main content

Grit

A lightweight, zero-dependency resilience toolkit for Python applications (sync and async).

In distributed systems, external dependencies like databases, microservices, and HTTP APIs will fail. Grit provides failure-handling primitives to keep your application stable during outages, network spikes, and service degradation.

What it's for

Grit helps you write fault-tolerant Python applications by addressing common distributed systems problems:

  • Transient Errors: Network blips or temporary HTTP 503s that recover after a quick retry.
  • Cascading Failures: An offline database blocking worker threads and taking down the whole system.
  • Resource Exhaustion: Too many concurrent incoming requests crashing memory or CPU.
  • Upstream Rate Limits: Exceeding rate limits on external APIs (e.g., Stripe, OpenAI).
  • Service Degradation: Knowing when to fail fast or return stale cached data instead of timing out.

Key Features

  • Sync & Async Native: Every primitive works seamlessly on sync functions, standard threads, and asyncio coroutines.
  • Zero External Dependencies: Implemented using standard library modules (asyncio, threading, time, signal).
  • Composable: Combine multiple resilience patterns into a single policy decorator.
  • Flexible Backoffs: Includes constant, linear, exponential, fibonacci, full jitter, and decorrelated jitter backoff algorithms.

Installation

pip install grit-toolkit

Quick Usage

Retry

from grit import retry, expo, decorr

@retry(tries=3, wait=expo(base=0.1, cap=5.0))
def fetch_user(user_id):
    return api.get(f"/users/{user_id}")

Circuit Breaker

from grit import breaker

db_breaker = breaker(fails=5, reset_after=30.0, name="postgres")

@db_breaker
def query_db():
    ...

Timeout

from grit import timeout

@timeout(2.5)
def heavy_computation():
    ...

Rate Limiter

from grit import limiter

lim = limiter(rate=10, per=1.0, burst=20)

@lim
def make_api_request():
    ...

Bulkhead

from grit import bulkhead

@bulkhead(size=5, timeout=1.0)
async def process_job():
    ...

Fallback

from grit import fallback

@fallback(value={"status": "offline"})
def get_live_feed():
    raise ConnectionError("Service unreachable")

Composing Policies

from grit import policy, fallback, timeout, retry, breaker, constant

api_policy = policy(
    fallback(value={"cached": True}),
    timeout(3.0),
    retry(tries=3, wait=constant(0.2)),
    breaker(fails=5, reset_after=15.0)
)

@api_policy
def get_data():
    ...

Running Tests

pytest

Metadata

Release files for grit-toolkit 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grit-toolkit 0.2.0
File Size Uploaded Provenance Actions
grit_toolkit-0.2.0.tar.gz 11.2 kB Without provenance Details for grit_toolkit-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for grit-toolkit 0.2.0
File Interpreter ABI Platform Actions
grit_toolkit-0.2.0-py3-none-any.whl Python 3 none any Details for grit_toolkit-0.2.0-py3-none-any.whl

Total release size: 21.9 kB

Release files / grit_toolkit-0.2.0.tar.gz

Download URL grit_toolkit-0.2.0.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
49364de29c787dda8fdba1ded36436183d7cd8c3580d5385ae29e9eeb3bbd22b
BLAKE2b-256 checksum
How to use checksums
8c4be2990cd17c14c2c001ac76ef532e205f1c173e70bf9ee7a497a36ac17e77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / grit_toolkit-0.2.0-py3-none-any.whl

Download URL grit_toolkit-0.2.0-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c91e750f3a793bec9737dbb641aeed19ceb1a4e0add0465a678b243df55735ce
BLAKE2b-256 checksum
How to use checksums
36e403a72eb12a6c0b3ac8f8c2f9c55f518e9522c1889a7e72c5d9a551fa9794
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for grit-toolkit 0.2.0

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page