In this code I have managed Asynchronous Paho Python MQTT communication synchronously by using Queue and while loop.
MTTQ broker https://mosquitto.org/
Paho client https://pypi.org/project/paho-mqtt/
# python3.6
import random
from paho.mqtt import client as mqtt_client
from queue import Queue
import datetime
import time
broker = '127.0.0.1'#'broker.emqx.io'
port = 1883
topic_1 = "Parent/Topic1"
topic_2 = "Parent/Topic2"
msg1_start = "topic1: start"
msg1_started = "topic1: started"
msg1_finished = "topic1: finished"
msg2_start = "topic2: start"
msg2_started = "topic2: started"
msg2_finished = "topic2: finished"
# Generate a Client ID with the subscribe prefix.
client_id = f'subscribe-{random.randint(0, 100)}'
# username = 'emqx'
# password = 'public'
msgQueue = Queue()
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(f"{datetime.datetime.now()} [Info] Connected to MQTT Broker!")
else:
print(f"{datetime.datetime.now()} [Error] Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
# client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"{datetime.datetime.now()} [Info] Received `{msg.payload.decode()}` from `{msg.topic}` topic")
msgQueue.put(msg)
client.subscribe(topic_1)
client.subscribe(topic_2)
client.on_message = on_message
def publish(client,topic,msg):
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"{datetime.datetime.now()} [Info] Send `{msg}` to topic `{topic}`")
else:
print(f"{datetime.datetime.now()} [Error] Failed to send message to topic {topic}")
def run():
client = connect_mqtt()
time.sleep(1)
publish(client,topic_1,msg1_start)
subscribe(client)
#client.loop_forever()
client.loop_start()
while True:
message = msgQueue.get()
if message is None:
continue
print(f"{datetime.datetime.now()} [Info] received from queue",str(message.payload.decode("utf-8")))
if message.topic == topic_1 and message.payload.decode() == msg1_finished:
publish(client,topic_2,msg2_start)
elif message.topic == topic_2 and message.payload.decode() == msg2_finished:
break
client.loop_stop() #client thread needs to be stopped when topic2 completed.
if __name__ == '__main__':
run()