Skip to main content

Propan framework: the simplest way to work with a messaging queues

Project description

Propan logo

Tests coverage Coverage Package version downloads
Supported Python versions GitHub

Propan

Propan - just an another one HTTP a declarative Python MQ framework. It's following by fastapi, simplify Message Brokers around code writing and provides a helpful development toolkit, which existed only in HTTP-frameworks world until now.

It's designed to create reactive microservices around Messaging Architecture.

It is a modern, high-level framework on top of popular specific Python brokers libraries, based on pydantic and fastapi, pytest concepts.


Documentation: https://lancetnik.github.io/Propan/


The key features are

  • Simple: Designed to be easy to use and learn.
  • Intuitive: Great editor support. Autocompletion everywhere.
  • Dependencies management: Minimization of code duplication. Access to dependencies at any level of the call stack.
  • Integrations: Propan is fully compatible with any HTTP framework you want
  • MQ independent: Single interface to popular MQ:
  • RPC: The framework supports RPC requests over MQ, which will allow performing long operations on remote services asynchronously.
  • Great to develop: CLI tool provides great development experience:
    • framework-independent way to manage the project environment
    • application code hot reload
    • robust application templates
  • Documentation: Propan automatically generates and presents an interactive AsyncAPI documentation for your project
  • Testability: Propan allows you to test your app without external dependencies: you do not have to set up a Message Broker, you can use a virtual one!

Supported MQ brokers

async sync
RabbitMQ :heavy_check_mark: stable :heavy_check_mark: :mag: planning :mag:
Redis :heavy_check_mark: stable :heavy_check_mark: :mag: planning :mag:
Nats :heavy_check_mark: stable :heavy_check_mark: :mag: planning :mag:
Kafka :warning: beta :warning: :mag: planning :mag:
SQS :warning: beta :warning: :mag: planning :mag:
NatsJS :hammer_and_wrench: in progress :hammer_and_wrench: :mag: planning :mag:
MQTT :mag: planning :mag: :mag: planning :mag:
Redis Streams :mag: planning :mag: :mag: planning :mag:
Pulsar :mag: planning :mag: :mag: planning :mag:

Community

If you are interested in this project, please give me feedback by star or/and watch repository.

If you have any questions or ideas about features to implement, welcome to discussions.


Declarative?

With declarative tools you can define what you need to get. With traditional imperative tools you must write what you need to do.

Take a look at classic imperative tools, such as aio-pika, pika, redis-py, nats-py, etc.

This is the Quickstart with the aio-pika:

import asyncio
import aio_pika

async def main():
    connection = await aio_pika.connect_robust(
        "amqp://guest:guest@127.0.0.1/"
    )

    queue_name = "test_queue"

    async with connection:
        channel = await connection.channel()

        queue = await channel.declare_queue(queue_name)

        async with queue.iterator() as queue_iter:
            async for message in queue_iter:
                async with message.process():
                    print(message.body)

asyncio.run(main())

aio-pika is a great tool with a really easy learning curve. But it's still imperative. You need to connect, declare channel, queues, exchanges by yourself. Also, you need to manage connection, message, queue context to avoid any troubles.

It is not a bad way, but it can be much easier.

from propan import PropanApp, RabbitBroker

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")

app = PropanApp(broker)

@broker.handle("test_queue")
async def base_handler(body):
    print(body)

This is the Propan declarative way to write the same code. That is so much easier, isn't it?


Quickstart

Install using pip:

pip install "propan[async-rabbit]"
# or
pip install "propan[async-nats]"
# or
pip install "propan[async-redis]"
# or
pip install "propan[async-kafka]"
# or
pip install "propan[async-sqs]"

Basic usage

Create an application with the following code at serve.py:

from propan import PropanApp
from propan import RabbitBroker
# from propan import RedisBroker
# from propan import NatsBroker
# from propan import SQSBroker
# from propan import KafkaBroker

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
# broker = NatsBroker("nats://localhost:4222")
# broker = RedisBroker("redis://localhost:6379")
# broker = SQSBroker("http://localhost:9324", ...)
# broker = KafkaBroker("localhost:9092")

app = PropanApp(broker)

@broker.handle("test")
async def base_handler(body):
    '''Handle all default exchange messages with `test` routing key'''
    print(body)

And just run it:

propan run serve:app

Type casting

Propan uses pydantic to cast incoming function arguments to types according to their annotation.

from pydantic import BaseModel
from propan import PropanApp, Context, RabbitBroker

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
app = PropanApp(broker)

class SimpleMessage(BaseModel):
    key: int

@broker.handle("test2")
async def second_handler(body: SimpleMessage):
    assert isinstance(body.key, int)

Dependencies

Propan a has dependencies management policy close to pytest fixtures. You can specify in functions arguments which dependencies you would to use. Framework passes them from the global Context object.

Already existed context fields are: app, broker, context (itself), logger and message. If you call not existing field, raises pydantic.ValidationError value.

But you can specify your own dependencies, call dependencies functions (like Fastapi Depends) and more.

import aio_pika
from propan import PropanApp, RabbitBroker, Context, Depends

rabbit_broker = RabbitBroker("amqp://guest:guest@localhost:5672/")

app = PropanApp(rabbit_broker)

async def dependency(body: dict) -> bool:
    return True

@rabbit_broker.handle("test")
async def base_handler(body: dict,
                       dep: bool = Depends(dependency),
                       broker: RabbitBroker = Context()):
    assert dep is True
    assert broker is rabbit_broker

Project Documentation

Propan automatically generates documentation for your project according to the AsyncAPI specification. You can work with both generated artifacts and place a Web view of your documentation on resources available to related teams.

The availability of such documentation significantly simplifies the integration of services: you can immediately see what channels and message format the application works with. And most importantly, it doesn't cost you anything - Propan has already done everything for you!

HTML-page


CLI power

Propan has its own CLI tool that provided the following features:

  • project generation
  • multiprocessing workers
  • project hot reloading
  • custom command line arguments passing

Context passing

For example: pass your current .env project setting to context

propan run serve:app --env=.env.dev
from propan import PropanApp, RabbitBroker
from propan.annotations import ContextRepo
from pydantic import BaseSettings

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")

app = PropanApp(broker)

class Settings(BaseSettings):
    ...

@app.on_startup
async def setup(env: str, context: ContextRepo):
    settings = Settings(_env_file=env)
    context.set_global("settings", settings)

Project template

Also, Propan CLI is able to generate a production-ready application template:

propan create async rabbit [projectname]

Notice: project template require pydantic[dotenv] installation.

Run the created project:

# Run rabbimq first
docker compose --file [projectname]/docker-compose.yaml up -d

# Run project
propan run [projectname].app.serve:app --env=.env --reload

Now you can enjoy a new development experience!


HTTP Frameworks integrations

Any Framework

You can use Propan MQBrokers without PropanApp. Just start and stop them according to your application lifespan.

from propan import NatsBroker
from sanic import Sanic

app = Sanic("MyHelloWorldApp")
broker = NatsBroker("nats://localhost:4222")

@broker.handle("test")
async def base_handler(body):
    print(body)

@app.after_server_start
async def start_broker(app, loop):
    await broker.start()

@app.after_server_stop
async def stop_broker(app, loop):
    await broker.close()

FastAPI Plugin

Also, Propan can be used as part of FastAPI.

Just import a PropanRouter you need and declare the message handler using the @event decorator. This decorator is similar to the decorator @handle for the corresponding brokers.

from fastapi import Depends, FastAPI
from pydantic import BaseModel
from propan.fastapi import RabbitRouter

router = RabbitRouter("amqp://guest:guest@localhost:5672")

app = FastAPI(lifespan=router.lifespan_context)

class Incoming(BaseModel):
    m: dict

def call():
    return True

@router.event("test")
async def hello(m: Incoming, d = Depends(call)) -> dict:
    return { "response": "Hello, Propan!" }

app.include_router(router)

Examples

To see more framework usages go to examples/

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

propan-0.1.3.5.tar.gz (71.0 kB view details)

Uploaded Source

Built Distribution

propan-0.1.3.5-py3-none-any.whl (108.6 kB view details)

Uploaded Python 3

File details

Details for the file propan-0.1.3.5.tar.gz.

File metadata

  • Download URL: propan-0.1.3.5.tar.gz
  • Upload date:
  • Size: 71.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for propan-0.1.3.5.tar.gz
Algorithm Hash digest
SHA256 920f00a4e41ae0b4f4c8e636decbf4f29749cf8cc1345104e4db0d056d33bf55
MD5 82d2bd6335ffaec9b94c7080a3cdedf5
BLAKE2b-256 36f3b059b125df936b4c45ca02e8f2834046e97fe0112500a5f91811960bdf01

See more details on using hashes here.

File details

Details for the file propan-0.1.3.5-py3-none-any.whl.

File metadata

  • Download URL: propan-0.1.3.5-py3-none-any.whl
  • Upload date:
  • Size: 108.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for propan-0.1.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7a89df765b459908997145b5c5029f1f1059adab296d54f71e33c8d38121619f
MD5 dd9d8db61a45d0b24a3901b454de8461
BLAKE2b-256 563d2686052e712f3999a3656c128cac7dada960c4fb095b841fb2a1253ceebc

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