Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Struggling with duplicate triggers #580

Unanswered
FredericG-BE asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

I think this one is related to #557 and #539

You must be logged in to vote
1 reply
Comment options

Thanks for the pointers. I am however not convinced this is the problem I am facing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /