MQTT Paho client Python – Stack Overflow
Currently the library does not provide a function/property that would provide you with the number of messages in the queue. However, you will get a MQTTErrorCode.MQTT_ERR_QUEUE_SIZE if you call publish whilst the queue is at/over capacity (so that may provide the info you need). The author of this issue appears to have similar aims so you may wish to comment/support that issue.
The queue is effectively the contents of _out_messages as can be seen here in the publish function:
if self._max_queued_messages > 0 and len(self._out_messages) >= self._max_queued_messages:
message.info.rc = MQTTErrorCode.MQTT_ERR_QUEUE_SIZE
return message.info
There is nothing to stop you from doing the same type of check in your code, but please do note that the leading underscore signals that the variable is intended to be private (so may be changed in future releases), and also take care referencing this if you are running the message loop in a seperate thread (use _out_message_mutex).
Read more here: Source link
