在某些脚本中我有一部分python代码无法正常工作。 我使用on_message回调来订阅主题,但是经过几个小时(介于8小时到两天之间的任何时间)之后,不再处理任何mqtt消息。我什至再也看不到mqtt代理的套接字,所以我猜想哪里有重试或重新连接丢失的地方?
从下面的脚本中,我仍然可以看到“正在尝试循环”消息的出现。
我在on消息回调中有一个try-except,因为我注意到那里的任何异常也会导致终止与mqtt代理的连接。
此代码在多个脚本中运行,问题无处不在。它与硬件或连接无关。
def start_keyble(command):
print("start_keyble with value " + command)
cmd = '/usr/local/bin/keyble-sendcommand --address XXXXXXXXXXXX --user_id 1 --user_key YYYYYYYYYYYYYYYY --command ' + str(command)
output = os.popen(cmd).readlines()
print("start_keyble will return ")
print(output)
return output
def on_message(client, userdata, message):
try:
if (str(message.topic).endswith("/set") and str(message.payload.decode("utf-8")) != ''):
param = str(message.topic)
value = str(message.payload.decode("utf-8"))
print("set parameter " + param + " with value " + value + " requested")
newstate = start_keyble(value)
#mqttc.publish("Garagentuere/status", str(newstate), 0)
for line in newstate:
if (line != ""):
mqttc.publish("Garagentuere/status", str(line), 0)
time.sleep(1)
except Exception:
return
if __name__ == '__main__':
...
mqttw = paho.Client('equiva-watcher', clean_session=True)
mqttw.on_message=on_message
mqttw.connect(BROKER_HOST, BROKER_PORT, 60)
mqttw.loop_start()
mqttw.subscribe([("Garagentuere/status/set", 0)])
while True:
try:
print("in try-loop")
time.sleep(FREQUENCY)
except KeyboardInterrupt:
break
except Exception:
raise