Paho on_connect 在连接正常时工作,但在错误时不调用。为什么?

Paho on_connect works if connection OK but is not called when error. Why?

为什么我的 paho-mqtt (1.5.1) on_connect 可以在连接正常的情况下工作,但在出错时不调用它。为了进行测试,我在 VM (VBox) 中使用基于 Ubuntu 18.04 LTS 和 Xfcs 运行 的 Linux Lite 4.2。

class subscribemqtt:
      .....
   def on_connect(self, client, userdata, flags, rc):
        print ("ZZZZZZZZZZZZZ in on_connect")
        connectErrs = {.........}
        self.connectRc = rc
        self.connectReason = connectErrs[str(rc)]
        print ("$$$$$$$$$$$$$", self.connectRc, self.connectReason)
return
    def subscribe(self, arguments):
        ...........
        self.client = paho.Client(self.config['CLIENT_ID'])
        self.client.on_message = self.on_subscribe
        self.client.on_connect = self.on_connect
        print ("#############", self.on_connect)
        print ("XXXXXXXXXXXX calling self.client.connect(...)
        self.client.connect(self.config['HOST'],self.config['PORT'])
        print ("YYYYYYYYYYYYY calling self.client.loop_start()")
        self.client.loop_start()
        print ("AAAAAAAAAAAAA", self.connected)
        while not self.connected:
            time.sleep(0.1)
        print ("BBBBBBBBBBBBB", self.connected, self.connectRc)

当所有参数都正确时,on_connect 被调用:

############# <bound method subscribemqtt.on_connect of <__main__.subscribemqtt object at 0x7f32065a6ac8>>
XXXXXXXXXXXX calling self.client.connect(self.config['HOST'],self.config['PORT']
YYYYYYYYYYYYY calling self.client.loop_start()
AAAAAAAAAAAAA**ZZZZZZZZZZZZZ in on_connect** 
False$$$$$$$$$$$$$
 0 Connection successful
BBBBBBBBBBBBB True 0

当我将主机地址设置为无效地址(创建错误以测试我的错误处理)时,我得到:

subscribemqtt.subscribe:topic= Immersion Dummy
############# <bound method subscribemqtt.on_connect of <__main__.subscribemqtt object at 0x7ffb942ae0b8>>
XXXXXXXXXXXX calling self.client.connect(self.config['HOST'],self.config['PORT']
Traceback (most recent call last):
  File "/home/linuxlite/Desktop/EMS/sendroutines/subscribemqtt.py", line 275, in <module>
    (retcode, reason, topic) = subscribeObj.subscribe([None, topic])
  File "/home/linuxlite/Desktop/EMS/sendroutines/subscribemqtt.py", line 191, in subscribe
    self.client.connect(self.config['HOST'],self.config['PORT'])
  File "/usr/local/lib/python3.6/dist-packages/paho/mqtt/client.py", line 941, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.6/dist-packages/paho/mqtt/client.py", line 1075, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python3.6/dist-packages/paho/mqtt/client.py", line 3546, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/lib/python3.6/socket.py", line 704, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
>>> 

感谢阅读。 艾伦

PS。我刚试过:

        try:
             self.client.connect(self.config['HOST'],self.config['PORT'])
        except:
            print ("**************** Error exception calling self.client.connect")

这行得通,但我的理解是 on_connect 应该调用错误。

来自docs

on_connect()

on_connect(client, userdata, flags, rc)

Called when the broker responds to our connection request.

重要的部分是“当经纪人回应时”。但是在示例中,您显示提供的主机名无法解析,因此代理永远不会响应,因为它从未真正联系过。

on_connect() 如果连接成功或由于 username/password 错误或协议版本不可用而失败(例如,从仅支持 v3 的代理请求 MQTTv5)