Description
My simple script for test without any logic only get message and send message. Hangs my script on wait_for_publish() or if i set ex. wait_for_publish(1) after 1 seconds goes further but message is published on server but info.is_published() return False
I made some tests and if I turn off client.loop_forever() and only make publish_data_to_broker() then everything works
broker = config.mqtt_broker
def on_connect(client, userdata, flags, rc, properties):
if rc == 0:
print("connected to topics")
client.subscribe(config.main_topic + config.bileterka + "/#")
client.subscribe(config.main_topic + config.terminal + "/#")
else:
print("errror")
def publish_data_to_broker(topic, msg, retain):
global ostatnia_wiadomosc
"""
Publikuje dane do brokera
:param: topic, msg, retain
"""
print(topic + " " + msg)
info = client.publish("test", msg, qos=2, retain=retain)
info.wait_for_publish()
print(info)
if info.is_published():
print("published OK")
return True
else:
logging.info(f"Pub fail {topic} {msg}")
return False
def on_message(client, userdata, msg):
msgIn = msg.payload.decode("utf-8")
try:
if "numerrej" in msg.topic:
print("----")
publish_data_to_broker(msg.topic, "test", False)
except Exception as e:
logging.error(f"on message {e}")
if __name__ == "__main__":
client.username_pw_set(username=config.mqtt_login, password=config.mqtt_passwd)
client.on_connect = on_connect
client.on_message = on_message
try:
client.connect(broker, config.mqtt_port, config.mqtt_keepalive)
except:
logging.info("ERROR: Connect error")
client.loop_forever()
Why wait_for_publish not work and is_publish return False with loop_forever ?
Is it possible to use wait_for_publish with loop or should i use threads?
Ubuntu server 24.04.01LTS
Python 3.10.12
paho-mqtt 2.1.0
mosquitto server 2.0.20