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 helpful development toolkit, existed only at HTTP-frameworks world until now.

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

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


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

Sources: https://github.com/Lancetnik/Propan/


The key features are

  • Easy: Designed to be easy to use and learn.
  • Intuitive: Great editor support. Autocompletion everywhere.
  • Dependencies management: Minimize code duplication. Multiple features from each argument and parameter declaration.
  • Integrations: Propan is ready to using in pair with any http framework you want
  • MQ independent: Single interface to popular MQ:
  • Greate to develop: cli tool provides great development expireince:
    • framework-independent way to rule application environment
    • application code hot reloading

Supported MQ brokers

async sync
RabbitMQ :heavy_check_mark: stable :heavy_check_mark: :mag: planning :mag:
Nats :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 :mag: planning :mag: :mag: planning :mag:
Kafka :mag: planning :mag: :mag: planning :mag:
SQS :mag: planning :mag: :mag: planning :mag:

Community

If you are interested at 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 or publick telegram group.


Declarative?

At declarative tools you should define what you need to get. At traditional imperative tools you should write what you need to do.

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

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 really 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 easy.

from propan import PropanApp
from propan.brokers.rabbit import 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]"

Basic usage

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

from propan import PropanApp
from propan.brokers.rabbit import RabbitBroker
# from propan.brokers.nats import NatsBroker

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
# broker = NatsBroker("nats://localhost:4222")

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 their annotation.

from pydantic import BaseModel
from propan import PropanApp, Context
from propan.brokers.rabbit import 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 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 existed field, raises pydantic.error_wrappers.ValidationError value.

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

from logging import Logger

import aio_pika
from propan import PropanApp, Context
from propan.brokers.rabbit import RabbitBroker

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

app = PropanApp(rabbit_broker)

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

CLI power

Propan has own cli tool 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
from propan.annotations import ContextRepo
from propan.brokers.rabbit import RabbitBroker
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_context("settings", settings)

Project template

Also propan cli is able to generate production-ready application template:

$ propan create [projectname]

Notice: project template require pydantic[dotenv] installation.

Run 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

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

from contextlib import asynccontextmanager

from fastapi import FastAPI
from propan.brokers.rabbit import RabbitBroker

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

app = FastAPI()

@asynccontextmanager
async def lifespan(app: FastAPI):
    await broker.start()
    yield
    await broker.close()

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

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

Uploaded Source

Built Distribution

propan-0.0.9.0-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for propan-0.0.9.0.tar.gz
Algorithm Hash digest
SHA256 fd704b34cc403dbc56c989cd7e144df30e8376f0f98c78168681d9c4ac534262
MD5 b548611b7413615e6ccbedc79920a2c2
BLAKE2b-256 dc52b3b2ec200a164a931948b56b3b1b23e6c51cb0c710fbe48b0917d13e1606

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for propan-0.0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ded1d136e63c0e0bd5aaa1d8cffda7aa2ba4fe4e91220ce3e720803474670b3
MD5 421a05d6bd0d0ee4efbe8530e3e02d24
BLAKE2b-256 310dc8456c908356d16f9ef469396ad8cfbb99a06b288af3a40f889c99a63d48

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