peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
549 stars 116 forks source link

Return to main application when errors are found #62

Closed Tangerino closed 2 years ago

Tangerino commented 2 years ago

Hi.

This library works like a charm. My only comment is in the function:

async def _keep_connected(self):

When there is an error, for instance, If the broker is not available, the client.publish does not ever return.

This is bad since the client may need to do other stuff, such as store the payload to the disk and retry later.

Can we add one more argument and tell the library to generate an exception so the client can take the actions accordingly?

Or add a timeout in:

    # Await broker connection.
    async def _connection(self):
        while not self._isconnected:
            await asyncio.sleep(1)

Thanks

kevinkk525 commented 2 years ago

Sorry I don't have the strength to write a more detailed answer but as far as timeouts for .publish() goes, take a look at: https://github.com/peterhinch/micropython-mqtt/blob/master/mqtt_as/mqtt_as_timeout.py

And for a more advanced timeout implementation with optionally returning if the connection is down (but this one is on my fork, best to stick with the above link): https://github.com/kevinkk525/micropython-mqtt/blob/master/mqtt_as_timeout_concurrent.py