-
Notifications
You must be signed in to change notification settings - Fork 58
Struggling with duplicate triggers #580
-
The same test-code as for the other discussion I opened on failing to register on mqtt messages at startup, demonstrates another issue I am struggling with. This time when I reload an app after startup
In the modules dir I have a mqttTestLib.py defining a class "HaspManager" that registers on mqtt messages in its constructor. In the apps folder I have mqttTest.py where an instance of HaspManager is created.
When I save mqttTest.py, it gets reloaded and a new instance of HaspManager is created but the old instance is still receiving the mqtt messages. So with each message that comes in, multiple instances on HaspManager handle the message. I am confused about that, when the "hm" variable is replaced, I would think the old instance is destroyed and also the reference to the trigger function looses references.
When I put the class definition code together with the app, all is fine (no duplicate triggers on messages) after reloading the app. So there seems to be a key difference here...
mqttTestLib.py:
import time # if only we could do this in real life...
def trigFuncFactory_mqtt(topic, func):
# log.info(">>> Setting up trigger")
try:
@mqtt_trigger(topic)
def trigFunc(topic, payload):
func(topic, payload)
except:
log.info(">>> Setting up trigger - EXCEPTION")
log.info(f">>> Setting up trigger - Done trigFunc={trigFunc}")
return trigFunc
class HaspManager(object):
def __init__(self, plateName):
global trigFunc
self.ref = time.time()
log.info(f"HaspManager.__init__(plateName={plateName}) ref={self.ref}")
self.plateName = plateName
self.trigFunc = trigFuncFactory_mqtt("hasp/discovery/#", self.onDiscovery)
def onDiscovery(self, topic, payload):
log.info(f"HaspManager.onDiscovery(topic=\"{topic}\") ref={self.ref}")
mqttTest.py:
from mqttTestLib import *
hm = HaspManager("plate_test")
I was thinking that the class instance was not fully destroyed. For testing, I stored the trigger function in a global variable, so I was sure it would be replaced when a new instance would be created and the old trigger would be removed. However the same results...
import time # if only we could do this in real life...
trigFunc = None
def trigFuncFactory_mqtt(topic, func):
# log.info(">>> Setting up trigger")
try:
@mqtt_trigger(topic)
def trigFunc(topic, payload):
func(topic, payload)
except:
log.info(">>> Setting up trigger - EXCEPTION")
log.info(f">>> Setting up trigger - Done trigFunc={trigFunc}")
return trigFunc
class HaspManager(object):
def __init__(self, plateName):
global trigFunc
self.ref = time.time()
log.info(f"HaspManager.__init__(plateName={plateName}) ref={self.ref} trigFunc={trigFunc}")
self.plateName = plateName
#self.trigFunc = trigFuncFactory_mqtt("hasp/discovery/#", self.onDiscovery)
trigFunc = trigFuncFactory_mqtt("hasp/discovery/#", self.onDiscovery)
def onDiscovery(self, topic, payload):
log.info(f"HaspManager.onDiscovery(topic=\"{topic}\") ref={self.ref}")
Thanks for your help
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks for the pointers. I am however not convinced this is the problem I am facing...
Beta Was this translation helpful? Give feedback.