Python program recconect

Hi,
Maybe somebody can help me with Python code. I want to have a reconnect function.
Here is my connection code.

def on_connect(client, userdata, flags, rc, properties=None):
global is_connected, reconnect_attempts
if rc == 0:
print(f"Priėmimo patvirtinimas gautas su kodu {rc}. Prisijungta sėkmingai.“)
is_connected = True
reconnect_attempts = 0 # Nustatome bandymų skaičių į nulį po sėkmingo prisijungimo
else:
print(f"Nepavyko prisijungti su kodu {rc}. Bandoma dar kartą (bandymas {reconnect_attempts + 1})…”)
is_connected = False
client.reconnect()

When I connect, I get such a message in the terminal.


My reconnect code

def on_disconnect(client, userdata, flags, rc):
print(“Disconnected with result code: %s”, rc)
is_connected = False
reconnect_count, reconnect_delay = 0, FIRST_RECONNECT_DELAY
while reconnect_count < MAX_RECONNECT_COUNT:
print(“Reconnecting in %d seconds…”, reconnect_delay)
time.sleep(reconnect_delay)
try:
client.reconnect()
print(“Reconnected successfully!”)
is_connected = True
return
except Exception as err:
print(“%s. Reconnect failed. Retrying …”, err)
reconnect_delay *= RECONNECT_RATE
reconnect_delay = min(reconnect_delay, MAX_RECONNECT_DELAY)
reconnect_count += 1
print(“Reconnect failed after %s attempts. Exiting…”, reconnect_count)

In the terminal I get message


Reconnected successfully, but I don’t receive a message, although I know exactly what I sent.
Why I don’t get data? How improve my reconnect code?

It seems like there might be a scoping issue with your is_connected variable. In the on_disconnect function, when you set is_connected = False, it is likely creating a local variable instead of modifying the global one. To fix this, you can use the global keyword inside the function to explicitly refer to the global variable.

def on_disconnect(client, userdata, flags, rc):
    global is_connected
    print("Disconnected with result code:", rc)
    is_connected = False
    reconnect_count, reconnect_delay = 0, FIRST_RECONNECT_DELAY
    while reconnect_count < MAX_RECONNECT_COUNT:
        print("Reconnecting in %d seconds…" % reconnect_delay)
        time.sleep(reconnect_delay)
        try:
            client.reconnect()
            print("Reconnected successfully!")
            is_connected = True
            return
        except Exception as err:
            print("%s. Reconnect failed. Retrying …" % err)
            reconnect_delay *= RECONNECT_RATE
            reconnect_delay = min(reconnect_delay, MAX_RECONNECT_DELAY)
            reconnect_count += 1

    print("Reconnect failed after %d attempts. Exiting…" % reconnect_count)

Make sure to use global is_connected at the beginning of the on_disconnect function to indicate that you are modifying the global variable.

If the issue persists, you might also want to check if the client object retains the subscriptions or re-subscribe after reconnecting. If not, you may need to re-subscribe to the topics after the reconnection.

Additionally, make sure that the messages you expect are being published to the topics you are subscribed to. If there are no messages being published to the topics, you won’t receive any data even after a successful reconnection.