0

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?

hamso
  • 1
  • 2
  • Possible duplicate of [How to make Python script run as service?](https://stackoverflow.com/questions/16420092/how-to-make-python-script-run-as-service) – LeopoldVonBuschLight Sep 07 '17 at 12:27

1 Answers1

0

I think you have to build a checker method like class1.isAlive() which tells you if the class is waitng for requests. Also I think you have to build this in the while True loop and react than of failures.

Additionally, you could write your own event with a wait function. Wait is more CPU hungry but it is more responsive. See here for example. But it depends on your python version.

M Dennis
  • 177
  • 7
  • I tried to add such a checker functionality, but I'm not exactly shure how to du it right - I edited my post accordingly. – hamso Sep 08 '17 at 12:11