升级paho-mqtt 2.x后on_disconnect回调参数数量异常问题
Paho-MQTT 2.x升级后on_disconnect回调参数数量错误问题解决
问题描述
将Python项目升级至paho-mqtt 2.x版本后,已按官方文档适配各类回调函数,但on_disconnect()回调始终提示参数数量错误:系统显示传入了5个参数(预期为4个),但代码中该回调仅定义了4个参数,无法定位多余参数来源。原代码在paho-mqtt 1.x版本可正常运行。
原问题代码
def connect_mqtt(client_id, username, password) -> mqtt_client: def on_connect(client, userdata, flags, reason_code, properties): if reason_code == 0: writeLog("Connected to MQTT Broker: " + client._client_id.decode('utf-8'),0) if client._client_id.decode('utf-8') == client_id_p: globals()["flag_connected_p"] = True if client._client_id.decode('utf-8') == client_id_s: globals()["flag_connected_s"] = True writeLog("Subscribing to topic: " + topic_s + " With client: " + client._client_id.decode('utf-8'),0) subscribe(client, topic_s) else: writeLog("Failed to connect to client: " + client._client_id.decode('utf-8') + ", error code:" + str(reason_code),0) writeLog("1: Connection refused - incorrect protocol version",0) writeLog("2: Connection refused - invalid client identifier",0) writeLog("3: Connection refused - server unavailable",0) writeLog("4: Connection refused - bad username or password",0) writeLog("5: Connection refused - not authorised",0) def on_disconnect(client, userdata, reason_code, properties): if reason_code == 0: writeLog("Requested disconnect MQTT excuted sucesfully for client: !" + client._client_id.decode('utf-8'),0) else: writeLog("Unexcpected disconnect from MQTT for client: " + client._client_id.decode('utf-8') + ", rc=" + str(reason_code),0) if client._client_id.decode('utf-8') == client_id_p: globals()["flag_connected_p"] = False if client._client_id.decode('utf-8') == client_id_s: globals()["flag_connected_s"] = False def on_publish(client, userdata, mid, reason_codes, properties): writeLog("published message "+ str(mid) + " From client: " + client._client_id.decode('utf-8') + " sucesfully",1) client = mqtt_client.Client(mqtt.CallbackAPIVersion.VERSION2,client_id, CleanSession) client.username_pw_set(username, password) client.on_connect = on_connect client.on_disconnect = on_disconnect client.on_publish = on_publish client.max_inflight_messages_set(1000) client.connect(broker, port) return client
问题原因及解决方案
在Paho-MQTT 2.x的CallbackAPIVersion.VERSION2回调规范中,on_disconnect的正确参数签名是5个参数:client, userdata, reason_code, properties, disconnect_flags。你当前的代码只定义了4个参数,缺少最后一个disconnect_flags参数,导致系统传参时不匹配。
修改后的on_disconnect函数如下(即使暂时不需要使用disconnect_flags,也需要保留参数占位):
def on_disconnect(client, userdata, reason_code, properties, disconnect_flags): if reason_code == 0: writeLog("Requested disconnect MQTT excuted sucesfully for client: !" + client._client_id.decode('utf-8'),0) else: writeLog("Unexcpected disconnect from MQTT for client: " + client._client_id.decode('utf-8') + ", rc=" + str(reason_code),0) if client._client_id.decode('utf-8') == client_id_p: globals()["flag_connected_p"] = False if client._client_id.decode('utf-8') == client_id_s: globals()["flag_connected_s"] = False
这个参数是2.x版本新增的,用于传递断开连接的标志位信息,升级时需要同步适配该回调的参数数量。
内容的提问来源于stack exchange,提问作者Pepijn De Winter
相关产品推荐
相关产品推荐

