Skip to main content

Grelmicro is a tool-kit designed to streamline the development cloud-native applications with FastAPI.

Project description

Grelmicro

Grelmicro is a lightweight framework/toolkit for building async microservices in Python.

It is the perfect companion to FastAPI and FastStream, providing essential tools like leader election, distributed lock, and task scheduling for creating robust distributed systems.

PyPI - Version PyPI - Python Version uv Ruff mypy


Source Code: https://github.com/grelinfo/fastapi-apscheduler4


Toolkit Modules

Each Grelmicro module addresses common problems in building microservices and distributed systems.

The modules are fast, lightweight and fully typed, offering non-blocking operations by default using AnyIO for concurrency and async/await. Adapters are provided to run synchronous code in AnyIO Worker Threads, ensuring compatibility with both synchronous and asynchronous functions.

sync Module

The sync module provides synchronization primitives for distributed systems.

The primitives are technology agnostic, supporting multiple backends (see backends module).

The available primitives are:

  • Leader Election: A single worker is elected as the leader for performing tasks only once in a cluster.
  • Lock: A distributed lock that can be used to synchronize access to shared resources.

task Module

The task module provides a simple task scheduler that can be used to run tasks periodically.

Note: This is not a replacement for bigger tools like Celery, taskiq, or APScheduler. It is just lightweight, easy to use, and safe for running tasks in a distributed system with synchronization.

The key features are:

  • Fast & Easy: Offers simple decorators to define and schedule tasks effortlessly.
  • Interval Task: Allows tasks to run at specified intervals.
  • Synchronization: Controls concurrency using synchronization primitives to manage simultaneous task execution (see the sync module).
  • Dependency Injection: Use FastDepends library to inject dependencies into tasks.
  • Error Handling: Catches and logs errors, ensuring that task execution failures do not stop the scheduling.

backends Module

The backends module is the key of technology agnostic design of Grelmicro.

For now, it provides only a lock backend with Redis, PostgreSQL, and Memory for testing purposes.

Important: Although Grelmicro use AnyIO for concurrency, the backends generally depend on asyncio, therefore Trio is not supported.

Note: Feel free to create your own backend and contribute it. In the abc module, you can find the protocol for creating new backends.

Installation

pip install grelmicro

Examples

FastAPI Integration

  • Create a file main.py with:
from contextlib import asynccontextmanager
import typer
from fastapi import FastAPI

from grelmicro.backends.redis import RedisLockBackend
from grelmicro.sync import LeaderElection, Lock
from grelmicro.task import TaskManager

# === FastAPI ===
@asynccontextmanager
async def lifespan(app):
    # Start the lock backend and task manager
    async with lock_backend, task:
        yield

app = FastAPI(lifespan=lifespan)

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

# === Grelmicro ===
task = TaskManager()
lock_backend = RedisLockBackend("redis://localhost:6379/0")

# --- Ensure that only one say hello world at the same time ---
lock = Lock("say_hello_world")

@task.interval(1, sync=lock)
def say_hello_world_every_second():
    typer.echo("Hello World")


@task.interval(1, sync=lock)
def say_as_well_hello_world_every_second():
    typer.echo("Hello World")


# --- Ensure that only one worker is the leader ---
leader_election = LeaderElection("leader-election")
task.add_task(leader_election)

@task.interval(10, sync=leader_election)
def say_hello_leader_every_ten_seconds():
    typer.echo("Hello Leader")

Dependencies

Grelmicro depends on Pydantic v2+, AnyIO v4+, and FastDepends.

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 on asyncio).

postgres Dependencies

When you install Grelmicro with pip install grelmicro[postgres] it comes with:

  • asyncpg: The Python asyncio interface for PostgreSQL.

License

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

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.1.1.tar.gz (73.2 kB view details)

Uploaded Source

Built Distribution

grelmicro-0.1.1-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: grelmicro-0.1.1.tar.gz
  • Upload date:
  • Size: 73.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for grelmicro-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bfe7f93de8df3664ced6b4d1936d25c3197eba10146eb8daf60e38ac3884c414
MD5 13ea8f437c13daa1725bf8edb2d1a310
BLAKE2b-256 103f453c4b49765d3c72ef0764756f2a997f1064dfb615e715bbb1d73b857c7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for grelmicro-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 78cf220f0cbadb691f319b429d3a5dc00a6d46b93487a102c9cb2fe0f91976e5
MD5 770218405de6b5f5849cb2df8d81a18c
BLAKE2b-256 45f7cffe8a672ed0705f2d0a9013e3206951bbfe3091b01fdf14776ff7e61972

See more details on using hashes here.

Supported by

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