Python paho-mqtt delayed connection status

This code snippet:

import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect('localhost')
print(client.is_connected())

while not client.is_connected():
  client.loop()
  print(client.is_connected())

print(client.is_connected())
client.disconnect()

produces:

False
False
True
True

which indicates that upon return from connect() the connection status is not yet updated. The documentation (pypi.org/project/paho-mqtt/#connect-reconnect-disconnect) reads The connect() function connects the client to a broker. This is a blocking function. This suggests that the status should be accurate on return from connect(). Am I missing something? Is there another way to test if client is connected?

Read more here: Source link