我的理解是,回調排隊,但因爲我不是100%肯定我寫了一對腳本的測試行爲。
首先認購和回調:
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()
這裏是送消息來觸發這些回調腳本:
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()
當您運行這兩個腳本的第一個返回:
Test1 start
Test1 done
Test2 start
Test2 done
從這個測試我覺得它很自信,它會自動排隊回調。
來源
2017-08-17 06:22:39
blp