Skip to content

Basics

Separation of dependencies

Propan supports various message brokers using special classes:

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

Be careful! Different brokers require different dependencies. If you have not installed these dependencies, the imported broker will have the None value.

To install Propan with the necessary dependencies for your broker, select one of the installation options:

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

Broker Initialization

Data for connecting Propan Broker to your message broker can be transmitted in 2 ways:

  1. In the broker constructor

    from propan import RedisBroker
    broker = RedisBroker("redis://localhost:6379/")
    
  2. In the connect method

    from propan import RedisBroker
    broker = RedisBroker()
    ...
    await broker.connect("redis://localhost:6379/")
    

  1. In the broker constructor

    from propan import RabbitBroker
    broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
    
  2. In the connect method

    from propan import RabbitBroker
    broker = RabbitBroker()
    ...
    await broker.connect("amqp://guest:guest@localhost:5672/")
    
  1. In the broker constructor

    from propan import KafkaBroker
    broker = KafkaBroker("localhost:9092")
    
  2. In the connect method

    from propan import KafkaBroker
    broker = KafkaBroker()
    ...
    await broker.connect("localhost:9092")
    
  1. In the broker constructor

    from propan import SQSBroker
    broker = SQSBroker("http://localhost:9324")
    
  2. In the connect method

    from propan import SQSBroker
    broker = SQSBroker()
    ...
    await broker.connect("http://localhost:9324")
    
  1. In the broker constructor

    from propan import NatsBroker
    broker = NatsBroker("nats://localhost:4222")
    
  2. In the connect method

    from propan import NatsBroker
    broker = NatsBroker()
    ...
    await broker.connect("nats://localhost:4222")
    

In the simplest case, initializing through the constructor is enough for most use cases.

However, if you need more granularity in a complex scenario, for example, when configuring a project via environment variables, you may need the second option. The full example is described here.

Note

The parameters passed to connect override the parameters passed to the constructor. Be careful with this.

In addition, calling connect again will have no effect. Therefore, you do not have to worry that broker.start() call (used inside PropanApp to run the broker) will cause any errors.