grelmicro is a lightweight toolkit for building Python applications that need to coordinate work across processes
Project description
grelmicro
grelmicro is a lightweight toolkit for building Python applications that need to coordinate work across processes, workers, or services.
Documentation: https://grelinfo.github.io/grelmicro/
Source Code: https://github.com/grelinfo/grelmicro
Vision
grelmicro is for any Python application that needs to coordinate work across multiple processes: microservices, modular monoliths, multi-worker deployments, or traditional applications scaling beyond a single process.
-
Easy to use. Simple decorators and context managers that do the right thing out of the box. No boilerplate, no complex configuration.
-
Async by default. All I/O operations use
async/await. Async is the natural fit for applications that spend most of their time waiting on network and disk, and it integrates cleanly with FastAPI and FastStream. -
Backend-agnostic. Every feature is defined by a protocol, not tied to a specific technology. Swap Redis for PostgreSQL, or use the in-memory backend for tests, without changing application code.
-
Lightweight. A toolkit, not a framework. Pick the modules you need, ignore the rest. Minimal configuration: just register a backend and start using it.
The long-term goal is to grow grelmicro into an enterprise-grade toolkit, and eventually rewrite performance-critical components in Rust for better throughput and safety.
Overview
Cache
The cache module provides a @cached decorator with per-key stampede protection. Choose the cache that fits your use case: TTLCache for fast in-memory caching within a single process, or RedisCache for shared caching across multiple processes.
Synchronization Primitives
The sync module provides distributed coordination primitives, technology-agnostic across Redis, PostgreSQL, SQLite, Kubernetes, and in-memory backends.
- Lock: Distributed lock for synchronizing access to shared resources.
- Task Lock: Distributed lock for scheduled tasks with minimum and maximum hold times.
- Leader Election: Single-leader election for running tasks only once in a cluster.
Task Scheduler
The task module provides periodic task execution with optional distributed locking for at-most-once semantics across workers. Not a replacement for Celery or APScheduler: it is lightweight, easy to use, and built for coordination.
Resilience
The resilience module provides patterns to protect services from failures and overload.
- Circuit Breaker: Automatically detects repeated failures and temporarily blocks calls to unstable services.
- Rate Limiter: Limits the number of requests per time window using the GCRA algorithm with Redis or in-memory backends.
Logging
The logging module provides 12-factor-compliant logging with JSON/text formatting, configurable timezone, and environment variable configuration (LOG_LEVEL, LOG_FORMAT, LOG_TIMEZONE).
Tracing
The tracing module provides unified instrumentation inspired by Rust's tracing crate. The @instrument decorator creates OTel spans and enriches log records with structured context automatically.
Installation
pip install grelmicro
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.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", 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")
Dependencies
grelmicro depends on Pydantic v2+, AnyIO v4+, and FastDepends.
standard Dependencies
When you install grelmicro with pip install grelmicro[standard] it comes with:
orjson: A fast, correct JSON library for Python.
redis Dependencies
When you install grelmicro with pip install grelmicro[redis] it comes with:
redis-py: The Python interface to the Redis key-value store (the async interface depends onasyncio).
postgres Dependencies
When you install grelmicro with pip install grelmicro[postgres] it comes with:
asyncpg: The Pythonasynciointerface for PostgreSQL.
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grelmicro-0.12.0.tar.gz.
File metadata
- Download URL: grelmicro-0.12.0.tar.gz
- Upload date:
- Size: 58.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4985c3c4ef8188d4b570c4cf6ae12a8b93595eddea34840fc20edcdf073870c6
|
|
| MD5 |
6a0a1903c4f49f9e5541d8f178d3a829
|
|
| BLAKE2b-256 |
0125ab548a7246691812413a9fc276f975c12f3353e2b66c626d7c7020a873a3
|
Provenance
The following attestation bundles were made for grelmicro-0.12.0.tar.gz:
Publisher:
release.yml on grelinfo/grelmicro
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
grelmicro-0.12.0.tar.gz -
Subject digest:
4985c3c4ef8188d4b570c4cf6ae12a8b93595eddea34840fc20edcdf073870c6 - Sigstore transparency entry: 1247935435
- Sigstore integration time:
-
Permalink:
grelinfo/grelmicro@611645df386182ea28ff2d1e48d3ce032cb2a48e -
Branch / Tag:
refs/tags/0.12.0 - Owner: https://github.com/grelinfo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@611645df386182ea28ff2d1e48d3ce032cb2a48e -
Trigger Event:
release
-
Statement type:
File details
Details for the file grelmicro-0.12.0-py3-none-any.whl.
File metadata
- Download URL: grelmicro-0.12.0-py3-none-any.whl
- Upload date:
- Size: 90.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a40a0d43d8f48147d027ef2dd01503cda3aacad0a21f13e8e7a9d167235325c8
|
|
| MD5 |
9efbe04ca8fdd104b1086b6c6f5fad88
|
|
| BLAKE2b-256 |
6bd1dfe4672cfc7f1c4fea16da3a5a7d9b4a3e3ab8220fd6d9726cd517e6f566
|
Provenance
The following attestation bundles were made for grelmicro-0.12.0-py3-none-any.whl:
Publisher:
release.yml on grelinfo/grelmicro
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
grelmicro-0.12.0-py3-none-any.whl -
Subject digest:
a40a0d43d8f48147d027ef2dd01503cda3aacad0a21f13e8e7a9d167235325c8 - Sigstore transparency entry: 1247935506
- Sigstore integration time:
-
Permalink:
grelinfo/grelmicro@611645df386182ea28ff2d1e48d3ce032cb2a48e -
Branch / Tag:
refs/tags/0.12.0 - Owner: https://github.com/grelinfo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@611645df386182ea28ff2d1e48d3ce032cb2a48e -
Trigger Event:
release
-
Statement type: