[Python] Paho-mqtt with asyncio library

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I want to implement paho-mqtt in which it should process the incoming messages asynchronously.

    I have implement gmqtt with asyncio which runs perfectly fine, but as far as I understand paho-mqtt is better to use rather than gmqtt (Link : https://www.emqx.io/blog/comparision-of-python-mqtt-client).

    gmqtt w/ asyncio :

    def assign_callbacks_to_client(self, client):
    """ Helper function which sets up client's callbacks. """
    client.on_connect = self.on_connect
    client.on_message = self.on_message
    client.on_disconnect = self.on_disconnect
    client.on_subscribe = self.on_subscribe

    async def subscriber(self, mqtt_name):
    """ Connect to mqtt-broker. """
    sub_client = MQTTClient(mqtt_name)
    self.assign_callbacks_to_client(sub_client)
    logging.info("connecting")
    await sub_client.connect(host=config.MQTT_HOST, port=int(config.MQTT_PORT))
    return sub_client


    could you please let me know, how to implement paho with asyncio library? Will using loop_start asynchronously, as I understand everytime it executes it starts a new thread in background.

    Continue reading...

Compartilhe esta Página