I want to implement a Paho MQTT Python Service which is always running, receiving and sending messages. If an error occurs in any instance it should restart.
I implemented two classes which each start a threaded network loop with paho's loop_start(). These classes then have some callback functions which call other classes and so on.
For now i have a simple Python script which calls the classes and loops:
from one import one
from two import two
import time
one()
two()
while True:
if one.is_alive():
print("one is still alive")
else:
print("one died - do something!")
time.sleep(1)
And here my class "one":
import paho.mqtt.client as mqtt
import json
class one():
def __init__(self):
self.__client = mqtt.Client(client_id = "one")
self.__client.connect("localhost", 1883)
self.__client.subscribe("one")
self.__client.on_connect = self.__on_connect
self.__client.on_message = self.__on_message
self.__client.on_disconnect = self.__on_disconnect
self.__client.loop_start()
def __on_connect(self, client, userdata, flags, rc):
print("one: on_connect")
def __on_disconnect(self, client, userdata, flags, rc):
print("one: on_disconnect")
def __on_message(self, client, userdata, message):
str_message = message.payload.decode('utf-8')
message = json.loads(str_message)
print("one: on_message: " + str(message))
def is_alive(self):
return True
However - if I send a package which produces an error (a pickled message instead of json for example) my "is_alive"-function is still returning True but the paho-implementation is not responsive anymore. So no further messages are sent to on_message. So only a part of the class is still responsive!? Class "two" is still responsive and the script is running in the "while True" still.
How do i properly check the functionality of such a class?