A basis library for MQTT based microservices in Python
Project description
Python MQTT based microservice base daemon
Since this is a recurring task this project contains a base implementation for a simple MQTT based service that offers just some basic features required for simple MQTT based microservices:
- Daemonizing
- PID file handling
- Log file handling
- Handling signals like SIGHUP and SIGTERM
- Connecting to an MQTT service using the Paho MQTT client, keeping the connection active
- Processing JSON configuration files to gather MQTT configuration and reloading of that configuration
- Allowing to specify topics relative to a base topic
Services are able to simply subclass and implement their own message processing functions. This makes life a little bit easier. This repository also implements some client utilities that wrap the Paho MQTT library that implement a similar pattern.
Note that this library does not work when subscribing to a huge number of topics with different handlers since it uses an own callback registration mechanism that all messages go through. It also does not work well when one wants to dynamically register or unregister from topics. Also in it's current implementation it linearly iterates over all registered handlers for each message and does not use any kind of trie.
Usage
The most simple usage possible just registers topic handlers and reacts to the received messages.
from mqttservice import mqttbaseservice
class EchoService(mqttbaseservice.MQTTBaseService):
def __init__(self):
super().__init__(
applicationName = "MQTTEchoService",
topicHandlers = [
{ 'topic' : 'echoservice/echo', 'handler' : [ self._handleEchoRequestMessage ] }
]
)
def _handleEchoRequestMessage(self, topic, msg):
self._logger.debug("Received echo request")
self.mqttPublish('echoservice/reply', { 'some' : "payload", 'will' : "be serialized", 'to' : "JSON from dict" })
def __enter__(self):
return self
def __exit__(self, type, value, tb):
pass
if __name__ == "__main__":
with EchoService() as service:
service.main()
Project details
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
Built Distribution
File details
Details for the file pymqttmicroservicebase-tspspi-0.0.1a0.tar.gz
.
File metadata
- Download URL: pymqttmicroservicebase-tspspi-0.0.1a0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0725af89d9fae33d27429bfb53556b18aceaae7c8b2b3e6ae6499ea3c882bf6d |
|
MD5 | fa6ef3c714a3f415e8aaff77e866953a |
|
BLAKE2b-256 | c74aa69ec48d6dd8f849b0c032120d418ba56e34d7e05de88e327f6c3e240944 |
File details
Details for the file pymqttmicroservicebase_tspspi-0.0.1a0-py3-none-any.whl
.
File metadata
- Download URL: pymqttmicroservicebase_tspspi-0.0.1a0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6be04b1dd131d9bd60e75b834a08ddac7bd6d98da686f757a86406c386493ba |
|
MD5 | edc7d0cbfd55b7dc97ae96e1985cac22 |
|
BLAKE2b-256 | 7bad72b87ce399f7e074c4afef461650005ef7de57e2611f14cc01271a4de35c |