disconnect() does not cause loop_forever to unblock
from the documentation:
def loop forever ... loop_forever() will handle reconnecting for you. If you call disconnect() in a callback it will return
I have a situation where I am trying to reinitialize with a new client ID, so I call client.disconnect() and client.reinitialise() with my new id from a message callback. In my main thread, I have a publisher that publishes once and waits for a response using loop forever (I can do it a few other ways, but this should work). My code looks something like this
import paho.mqtt.client as mqtt
import time
class waiting_client(mqtt.Client):
def __init__(*args, **kwargs):
super().__init__(*args, **kwargs)
self.connect("localhost", 1883)
time.sleep(5)
self.publish("send_me_new_client_id", payload=None, qos=2)
self.loop_forever()
on_message(self, mqttc, obj, msg):
self.disconnect()
self.reinitialise(client_id=str(msg.payload))
From the documentation, I would think it would be valid loop_forever() and disconnect() in this way. Now it does work when I call disconnect() then let the callback thread finish, but this is not ideal since I then have to handle it in the on_disconnect() callback and maintain a flag on whether I should reconnect with a new client_id.
Is there any way to force a reconnection (or at least break out of loop_forever()) like this without relying on my message callback to return?
Could you try repeating your test with the 1.6.x branch? I think it might work ok there.
I'm going to close this due to inactivity.
Note: This is part of an exercise to clean up old issues so that the project can move forwards. Due to the number of issues being worked through mistakes will be made; please feel free to reopen this issue (or comment) if you believe it's been closed in error.