[Python] Does on_message in paho mqtt run in a new thread?

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Lets assume I have subscribed to some topic and the mosquitto server is continuosly publishing some message on that topic.

    How does the on_message method get invoked when a new message arrives from mosquitto broker? Does it run on the main thread thereby blocking it and only processing the next message when the current one is processed or does it spawn a new thread with the on_message method every time a new message arrives?

    import paho.mqtt.client as mqtt

    def on_message(client, userdata, message):
    print message.payload
    return True

    topics = ["topic1",]
    client = mqtt.Client("testclient",protocol=mqtt.MQTTv31,clean_session=True)
    client.on_message=on_message
    client.connect("127.0.0.1", 1883, 60)
    for tpc in topics:
    client.subscribe(tpc,0)
    client.loop_forever()

    Continue reading...

Compartilhe esta Página