I'm using MQTT to disturb messages in my network and have an question about the cleanest way to publish and subscribe multiple messages to the broker.
First of all, I've got two lists:
request_list = [('sensors/system/temperature', 0),
('sensors/system/gyroscope', 1),
('sensors/system/acceleration', 2)]
Which contains my topics I have to publish my messages to.
My second list defines the messages I want to publish and the topics where I want to get my response (== the topics I have to subscribe to get my answers).
request_value = ['{"response":"similarity/sensors/system/temperature","duration":"60s"}',
{"response":"similarity/sensors/system/gyroscope","duration":"60s"}',
'{"response":"similarity/sensors/system/acceleration","duration":"60s"}']
My broker is for every topic the same and defined with HOST= "192.168.137.1" on PORT = "8083".
For now I'am using a for loop, to subscribe to one topic, publish my message and wait for the message to come in. Because I have to wait for every subscribtions and publish to suceed its very time consuming. The pseudocode of my current code looks like the following:
list_measurments = []
for topic in request_list:
client.connect("Broker","Host")
client.loop_start()
client.subscribe("sub_topic")
client.pub("pub_topic","pub_message")
client.callback("append list_measurements")
client.loop_stop() #stop the loop
client.disconnect
I tried to use threads form my question here but it turned out that the normal use of threads would be to publish the same message to a lot of different brokers. I also thought about multiple subscribtions.
If anybody could give me an hint, what the cleanest and fastest approach would be, I'd be very thankful.