Closed yateya closed 1 year ago
My first through is to rely on manual synchronization:
@datafile('broker.yml', manual=True)
class Broker:
mqtt_addr: str = 'localhost:1883'
def check_mqtt_addr(self):
if self.datafile.modified:
old_mqtt_addr = self.mqtt_addr
self.datafile.load()
if self.mqtt_addr != old_mqtt_addr:
mqtt_client.disconnect()
mqtt_client.connect(self.mqtt_addr)
broker = Broker()
mqtt_client = Client()
mqtt_client.connect(broker.mqtt_addr)
while True:
time.sleep(1)
broker.check_mqtt_addr()
Do you think that would work for your use case?
Thanks for the reply. Yes, it looks good. I will give it a try.
Hi,
Is there any way to run some code when object is updated after the yml file is updated?
In this example code, I want to reconnect to correct broker when broker address is modified in the yml file.
I found a work-around by checking
_last_load
attribute; which seems not a good option.Having a callback with old/new values will be very helpful. For example like this: