Moksha offers a Producer
API that allows you to easily provide data to
your message brokers. Producers are loaded and run by the MokshaHub
,
isolated from the WSGI application.
The Producers contain a connection to the MokshaHub via the self.hub object. It also provides a send_message(topic, message) method that will send your message to the hub.
The PollingProducer
will automatically wake up at a given frequency
(which can be a datetime.timedelta object, or the number of a seconds), and
call the poll()
method.
Below is an example of a PollingProducer
that wakes up every 10
seconds, and sends a ‘Hello World!’ message to the ‘hello’ topic.
from datetime import timedelta
from moksha.api.hub.producer import PollingProducer
class HelloWorldProducer(PollingProducer):
frequency = timedelta(seconds=10)
def poll(self):
self.send_message('hello', 'Hello World!')
To install your Producer, simply add it to the [moksha.producer] entry-point in your setup.py, like so:
[moksha.producer]
hello = myproject.producers:HelloWorldProducer