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.1.1

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.1.1
File Size Uploaded Provenance Actions
grit_toolkit-0.1.1.tar.gz 9.5 kB Without provenance Details for grit_toolkit-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 18.3 kB

Release files / grit_toolkit-0.1.1.tar.gz

Download URL grit_toolkit-0.1.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d82a1a8e5b307556f4cd790a07b7c330030666761d8bcbbfea0d7e56c70aa2cb
BLAKE2b-256 checksum
How to use checksums
a464c12acaed9d4b73bb0b144d7a60e2a728eca03d6f12273668d047c83ca652
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.1.1-py3-none-any.whl

Download URL grit_toolkit-0.1.1-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0530dffde2e35a27f549c32a00ecf871031040dc4422e140917a276b4348bad6
BLAKE2b-256 checksum
How to use checksums
ae5d496d9834f91b76274e81023ce4eb741a88b6d639765fb80e5ca75ae6de69
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.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.

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