Using the threaded client interface

Now, we will write a new version of the vehicle remote control application to use the threaded interface, also known as the threaded loop. Open the existing vehicle_mqtt_remote_control.py Python file and replace the lines that define the publish_command function with the following lines. The code file for the sample is included in the mqtt_python_gaston_hillar_05_04 folder, in the vehicle_mqtt_remote_control.py file:

def publish_command(client, command_name, key="", value=""): 
    command_message = build_command_message( 
        command_name, key, value) 
    result = client.publish(topic=commands_topic, 
    payload=command_message, qos=2) 
    time.sleep(1) 
    return result 

We removed the following line before the call to time.sleep(1) ...

Get Hands-On MQTT Programming with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.