Skip to main content

grelmicro is a lightweight toolkit for building Python applications that need to coordinate work across processes

Project description

grelmicro grelmicro

Micro by design. Fast by default. Import only what you need.

A toolkit of Python primitives built for production needs, for services that coordinate work across processes, workers, or clusters.

PyPI - Version PyPI - Python Version License: MIT codecov uv Ruff ty

Project status: Active development. grelmicro is pre-1.0, so APIs can change between minor releases. Every change ships with a clear deprecation path: DeprecationWarning, PEP 702 @deprecated, and a one-minor removal window.


Documentation: https://grelinfo.github.io/grelmicro/

Source Code: https://github.com/grelinfo/grelmicro


Why grelmicro

grelmicro gives you the building blocks every Python service needs: locks, rate limits, circuit breakers, cache, logging, health checks, and task scheduling. Each is a small, focused module with a pluggable backend.

It is built for any Python application, from a standalone script to full microservice patterns and self-contained systems, with a strong focus on solving distributed system problems. It fits naturally into cloud-native applications, containerized apps, and Kubernetes deployments.

  • Micro: one focused primitive per module. Import only what your code touches. Nothing else is loaded.
  • Fast: thin wrappers with low per-call overhead. Redis operations are server-side Lua scripts; in-memory paths skip I/O entirely.
  • Async-first: every I/O call is async / await. Drops into FastAPI, FastStream, and any AnyIO-based stack.
  • Backend-agnostic: each primitive is a protocol. Swap Redis for PostgreSQL or SQLite without touching application code.
  • Production-ready: 100% test coverage, full type hints, and explicit deprecation paths (DeprecationWarning + PEP 702 @deprecated) with a one-minor-version removal window.

Modules

Module Summary
Cache @cached decorator with per-key stampede protection. In-memory TTLCache or RedisCacheBackend.
Synchronization Distributed Lock, TaskLock, LeaderElection. Redis, PostgreSQL, SQLite, Kubernetes, in-memory.
Task Scheduler Periodic task execution with optional distributed locking. Lightweight, not a Celery replacement.
Resilience Circuit Breaker and Rate Limiter with pluggable algorithms (TokenBucket, GCRA).
Logging 12-factor logging with JSON, LOGFMT, TEXT, or PRETTY output, structured error rendering, and OpenTelemetry trace context.
Tracing Unified instrumentation. @instrument creates OpenTelemetry spans and enriches log records with structured context.
Health Health check registry with concurrent runners and FastAPI liveness / readiness integration.
JSON Fast JSON via orjson when available, with automatic fallback to stdlib json.

Installation

pip install grelmicro

See the Installation guide for uv and poetry commands, plus optional extras for Redis, PostgreSQL, SQLite, Kubernetes, OpenTelemetry, and structlog.

Example

FastAPI integration

Create a file main.py with:

import logging
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException, Request

from grelmicro.cache import JsonSerializer, TTLCache, cached
from grelmicro.cache.redis import RedisCacheBackend
from grelmicro.logging import configure_logging
from grelmicro.resilience import GCRA
from grelmicro.resilience.circuitbreaker import CircuitBreaker
from grelmicro.resilience.errors import RateLimitExceededError
from grelmicro.resilience.ratelimiter import RateLimiter
from grelmicro.resilience.redis import RedisRateLimiterBackend
from grelmicro.sync import LeaderElection, Lock
from grelmicro.sync.redis import RedisSyncBackend
from grelmicro.task import TaskManager

logger = logging.getLogger(__name__)

# === grelmicro ===
task = TaskManager()
sync_backend = RedisSyncBackend("redis://localhost:6379/0")
cache_backend = RedisCacheBackend("redis://localhost:6379/0", prefix="myapp:")
rate_limit_backend = RedisRateLimiterBackend("redis://localhost:6379/0")
leader_election = LeaderElection("leader-election")
task.add_task(leader_election)

cache = TTLCache(ttl=300, serializer=JsonSerializer())


# === FastAPI ===
@asynccontextmanager
async def lifespan(app):
    configure_logging()
    async with sync_backend, cache_backend, rate_limit_backend, task:
        yield


app = FastAPI(lifespan=lifespan)


# --- Cache: avoid redundant database queries ---
@cached(cache)
async def get_user(user_id: int) -> dict:
    return {"id": user_id, "name": "Alice"}


@app.get("/users/{user_id}")
async def read_user(user_id: int):
    return await get_user(user_id)


# --- Circuit Breaker: protect calls to an unreliable service ---
cb = CircuitBreaker("my-service")


@app.get("/")
async def read_root():
    async with cb:
        return {"Hello": "World"}


# --- Rate Limiter: protect endpoints from overload ---
api_limiter = RateLimiter("api", algorithm=GCRA(limit=100, window=60))


@app.get("/api")
async def api_endpoint(request: Request):
    try:
        await api_limiter.acquire_or_raise(key=request.client.host)
    except RateLimitExceededError as exc:
        raise HTTPException(
            status_code=429,
            detail="Too many requests",
            headers={"Retry-After": str(int(exc.retry_after))},
        )
    return {"status": "ok"}


# --- Distributed Lock: synchronize access to a shared resource ---
lock = Lock("shared-resource")


@app.get("/protected")
async def protected():
    async with lock:
        return {"status": "ok"}


# --- Interval Task: run locally on every worker ---
@task.interval(seconds=5)
def heartbeat():
    logger.info("heartbeat")


# --- Distributed Task: run once per interval across all workers ---
@task.interval(seconds=60, max_lock_seconds=300)
def cleanup():
    logger.info("cleanup")


# --- Leader-gated Task: only the leader executes ---
@task.interval(seconds=10, leader=leader_election)
def leader_only_task():
    logger.info("leader task")

License

This project is licensed under the terms of the MIT license.

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

grelmicro-0.14.2.tar.gz (69.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grelmicro-0.14.2-py3-none-any.whl (103.3 kB view details)

Uploaded Python 3

File details

Details for the file grelmicro-0.14.2.tar.gz.

File metadata

  • Download URL: grelmicro-0.14.2.tar.gz
  • Upload date:
  • Size: 69.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for grelmicro-0.14.2.tar.gz
Algorithm Hash digest
SHA256 65e6519964cb33f876bce60ad4bd73333215333c785f6240109863b5dc500080
MD5 a7567c5b42e245d5cf79a2a78ef9eaa2
BLAKE2b-256 c76ae9087edf0fcf2706c358181a37ee116bbd5111ed7923eaa55d30254b63aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for grelmicro-0.14.2.tar.gz:

Publisher: release.yml on grelinfo/grelmicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file grelmicro-0.14.2-py3-none-any.whl.

File metadata

  • Download URL: grelmicro-0.14.2-py3-none-any.whl
  • Upload date:
  • Size: 103.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for grelmicro-0.14.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53c0a8dd36b80a94969bcb9cddb7375997122c4c565556003913504bdf6e97af
MD5 587aaf0f2e4363027764f0657540b399
BLAKE2b-256 5cd6f4f24ea293a3fe832aa5568aa6de37499d372c6601a95f46b190f6feaf54

See more details on using hashes here.

Provenance

The following attestation bundles were made for grelmicro-0.14.2-py3-none-any.whl:

Publisher: release.yml on grelinfo/grelmicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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