0

I'm using the paho-mqtt library to write an MQTT script which triggers a callback function when there's a message published to a certain topic and I'm trying to determine if I need to write some sort of "message queue" functionality in order to defer execution of successive callbacks until the previous callback has returned.

The callback function can take a while to execute and there could be scenarios where another message is published to the trigger topic before the callback function has returned.

It seems that MQTT will defer it by itself with successive callbacks being stored up until the previous callback has returned but I was just hoping to get some confirmation on this.

Thanks

Aarron

1 Answers1

0

My understanding was that the callbacks queued but since I was not 100% certain I wrote a pair of scripts to test the behavior.

First the subscription and callbacks:

import paho.mqtt.client as mqtt
from time import sleep

def on_connect(client, userdata, flags, rc):
    client.subscribe('callback-test/#')

def test1(client, userdata, message):
    print("Test1 start")
    sleep(2)
    print("Test1 done")

def test2(client, userdata, message):
    print("Test2 start")
    sleep(2)
    print("Test2 done")

def on_log(client, userdata, level, buf):
    print(level, buf)

client = mqtt.Client()
client.on_connect = on_connect
client.message_callback_add('callback-test/1', test1)
client.message_callback_add('callback-test/2', test2)
client.on_log = on_log
client.connect("test.mosquitto.org")
client.loop_forever()

And here is the script to send messages to trigger those callbacks:

import paho.mqtt.client as mqtt

# noinspection PyShadowingNames,PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
def on_connect(client, userdata, flags, rc):
    client.publish('callback-test/1', "test1")
    client.publish('callback-test/2', "test2")

# noinspection PyShadowingNames,PyUnusedLocal,PyUnusedLocal
def on_log(client, userdata, level, buf):
    print(level, buf)

client = mqtt.Client()
client.on_connect = on_connect
client.on_log = on_log
client.connect("test.mosquitto.org")
client.loop_forever()

When you run both scripts the first one returns:

Test1 start
Test1 done
Test2 start
Test2 done

From this test I feel pretty confident that it is automatically queuing the callbacks.

blp
  • 578
  • 1
  • 5
  • 9