Skip to main content

Lightweight, efficient and developer-friendly framework for component communication

Project description

drawing

FastCC

Ruff Mypy Gitmoji

Framework for component communication with MQTT and Protocol Buffers :boom:.

  • Lightweight :zap:
  • Efficient :rocket:
  • Developer-friendly :technologist:

This framework is built on top of empicano's aiomqtt.

Example

# app.py
from __future__ import annotations

import asyncio
import contextlib
import logging
import os
import sys

import fastcc

router = fastcc.Router()


@router.route("greet")
async def greet(name: str, *, database: dict[str, int]) -> str:
    """Greet a user.

    Parameters
    ----------
    name
        The name of the user.
        Autofilled by fastcc.
    database
        The database.
        Autofilled by fastcc.

    Returns
    -------
    str
        The greeting message.
    """
    # ... do some async work
    await asyncio.sleep(0.1)

    database[name] += 1
    occurrence = database[name]
    return f"Hello, {name}! Saw you {occurrence} times!"


async def main() -> None:
    """Run the app."""
    logging.basicConfig(level=logging.INFO)

    database: dict[str, int] = {"Alice": 0, "Bob": 0}
    app = fastcc.FastCC("localhost")
    app.add_router(router)
    app.add_injector(database=database)
    app.add_exception_handler(
        KeyError,
        lambda e: fastcc.MQTTError(repr(e), 404),
    )

    await app.run()


# https://github.com/empicano/aiomqtt?tab=readme-ov-file#note-for-windows-users
if sys.platform.lower() == "win32" or os.name.lower() == "nt":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

with contextlib.suppress(KeyboardInterrupt):
    asyncio.run(main())
# client.py
from __future__ import annotations

import asyncio
import contextlib
import logging
import os
import sys

import fastcc

_logger = logging.getLogger(__name__)


async def main() -> None:
    """Run the app."""
    logging.basicConfig(level=logging.INFO)

    async with fastcc.Client("localhost") as client:
        try:
            response = await client.request(
                "greet",
                "Charlie",
                response_type=str,
            )
        except fastcc.MQTTError as e:
            details = f"An error occurred on the server: {e.message}"
            _logger.error(details)

        response = await client.request("greet", "Alice", response_type=str)
        _logger.info("response: %r", response)


# https://github.com/empicano/aiomqtt?tab=readme-ov-file#note-for-windows-users
if sys.platform.lower() == "win32" or os.name.lower() == "nt":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

with contextlib.suppress(KeyboardInterrupt):
    asyncio.run(main())

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

fastcc-4.0.3.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

fastcc-4.0.3-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file fastcc-4.0.3.tar.gz.

File metadata

  • Download URL: fastcc-4.0.3.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for fastcc-4.0.3.tar.gz
Algorithm Hash digest
SHA256 54af4b0665efb08118dd22cb22cc4403ffb3968dcfc16716f0cb5edb9abef491
MD5 a497515b94ed7de837918111dfce14b0
BLAKE2b-256 480bb49536927382ca527318d27e87150959fc324e43f01d50f2e054d0675b23

See more details on using hashes here.

File details

Details for the file fastcc-4.0.3-py3-none-any.whl.

File metadata

  • Download URL: fastcc-4.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for fastcc-4.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7b8381046a7be2a6e4d845c1ac8f6c8d62313771e07c6a59bae76927e7d90bef
MD5 bde13d6ff0d339ae773ff11e4d87861f
BLAKE2b-256 989f05c68c659f8d8421bbc5886394c4d43d1504056383c313a4146744c2104c

See more details on using hashes here.

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