PropanApp¶
If you are using the Propan CLI, you need to create an instance of the application for the project to work:
from propan import PropanApp
app = PropanApp()
Tip
When initializing, PropanApp
writes itself to ContextRepo
with the name "app"
, so you can always access it from context.
Using Brokers¶
In order for PropanApp
to launch your broker, you need to put it in the application object.
This is usually done when declaring the application itself:
from propan import PropanApp, RedisBroker
broker = RedisBroker("redis://localhost:6379")
app = PropanApp(broker)
from propan import PropanApp, RabbitBroker
broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
app = PropanApp(broker)
from propan import PropanApp, KafkaBroker
broker = KafkaBroker("localhost:9092")
app = PropanApp(broker)
from propan import PropanApp, SQSBroker
broker = SQSBroker("http://localhost:9324", ...)
app = PropanApp(broker)
from propan import PropanApp, NatsBroker
broker = NatsBroker("nats://localhost:4222")
app = PropanApp(broker)
But, sometimes you may need to initialize the broker elsewhere. In this case, you can use the app.set_broker
method:
from propan import PropanApp, RedisBroker
app = PropanApp()
@app.on_startup
def init_broker():
broker = RedisBroker("redis://localhost:6379")
app.set_broker(broker)
from propan import PropanApp, RabbitBroker
app = PropanApp()
@app.on_startup
def init_broker():
broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
app.set_broker(broker)
from propan import PropanApp, KafkaBroker
app = PropanApp()
@app.on_startup
def init_broker():
broker = KafkaBroker("localhost:9092")
app.set_broker(broker)
from propan import PropanApp, SQSBroker
app = PropanApp()
@app.on_startup
def init_broker():
broker = SQSBroker("http://localhost:9324", ...)
app.set_broker(broker)
from propan import PropanApp, NatsBroker
app = PropanApp()
@app.on_startup
def init_broker():
broker = NatsBroker("nats://localhost:4222")
app.set_broker(broker)
Launching other apps¶
If the broker is not passed to PropanApp
, the following functions will still work:
- Life Cycle Hooks
- Hot-reload code
- Multiprocessing of execution
In fact, as a broker, you can pass an instance of any class that will have asynchronous methods start
and close
:
class ABCBroker:
async def start(self) -> None:
...
async def close(self) -> None:
...
If your code satisfies this interface, PropanApp can be used as a convenient tool for project management.