Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wait_for_publish() never ends and is_publish return False #875

Open
kessero opened this issue Dec 16, 2024 · 0 comments
Open

wait_for_publish() never ends and is_publish return False #875

kessero opened this issue Dec 16, 2024 · 0 comments
Labels
Status: Available No one has claimed responsibility for resolving this issue.

Comments

@kessero
Copy link

kessero commented Dec 16, 2024

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

@github-actions github-actions bot added the Status: Available No one has claimed responsibility for resolving this issue. label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Available No one has claimed responsibility for resolving this issue.
Projects
None yet
Development

No branches or pull requests

1 participant