簡體   English   中英

被 paho-mqtt 中的約定混淆,將內置方法分配為我創建的 function(但沒有參數)

[英]Confused by convention in paho-mqtt for asigning a built in method as a function that i create (but without arguments)

我正在嘗試了解 paho-MQTT 庫。 我正在努力理解圖書館編碼中明顯的約定,但對我來說沒有意義。 如果有人能給我我應該尋找的主題,我很樂意查找。

很多 paho-mqtt 教程和 PAHO 基金會頁面 ( https://www.eclipse.org/paho/index.php?page=clients/python/docs/index.php#constructor-reinitialise ) 談論處理on_message ,和Client object 的on_connect方法(或者至少我認為是方法)。所有教程都提供了使用這些方法的標准方法,但我無法理解。 它是這樣的:

定義一個 function,它采用一組數字 arguments。類似於:

def on_message(client, userdata, message):  
    print(message.payload, 'on', message.topic)

然后的過程是創建一個“mqtt 客戶端”object 並將其連接到代理。 之后,要查看客戶端訂閱的消息,我執行以下操作:

client.on_message = on_message

這是我不明白的這一部分。 我理解這意味着我正在為一個方法分配 function 的值(但沒有調用它的 arguments 或指示它的函數)。 我原以為client.on_message會返回一個 3 元組,我會通過上面的 function 訪問它,如下所示:

on_message(client.on_message)

當我調用type(client.on_message)時,我得到NoneType回來,表明mqtt.Client.on_message沒有返回任何東西。 這解釋了為什么我不能在該方法上調用我的 function。

也許這只是一個語法問題,但有人可以在這里解釋約定(或告訴我應該查找什么)。 Client.on_message 是Client.on_message的方法嗎? 以及如何在不提供任何 arguments 的情況下為它分配我定義的 function 的值,盡管我在定義它們時指定了 arguments? 此外,我如何在不指明括號( on_message() )的情況下分配 function ?

下面是 paho-mqtt 的完整工作代碼:

#import the library
import paho.mqtt.client as mqtt 

#Write the function to get the payload content (i.e. the text) from the message object
def on_message(client, userdata, message):
    print('Recieved message', str(message.payload))

#create the mqtt object and connect to the broker
MQTT_BROKER = [broker-IP]
client = mqtt.Client('Client1')
client.connect(MQTT_BROKER)

#subscribe to the topic
client.subscribe('TEST_TOPIC')

#Somehow invoke the function defined above on the mqtt on.message method - i.e. what i don't understand
client.on_message = on_message

#Do this continually so i can keep looking for messages published on this topic
client.loop_forever()

我遇到的問題是我仍然無法在控制台中看到關於我訂閱的主題的消息。 我知道這些正在其他地方(在另一個客戶端上)發布,因為我可以在運行mosquitto_sub -t 'TEST_TOPIC'時在代理上看到它們。

目前,我只是想了解約定,以便進行故障排除。

您永遠不會顯式調用任何回調函數。 客戶端將在適當的時候從它的事件循環中調用它們。

client.on_message = on_message是告訴客戶端消息到達時(將來)呼叫哪個 function 的方式。 傳遞沒有 arguments 的 function 名稱會將句柄傳遞給 function 本身。

on_connect function 類似,這是一個 function,客戶端在完成與代理的連接后將調用它。

我已經重新安排了您的代碼,以確保事情以正確的順序完成,使用on_connect回調在完成連接后訂閱主題。

#import the library
import paho.mqtt.client as mqtt 

#Write the function to get the payload content (i.e. the text) from the message object
def on_message(client, userdata, message):
    print('Recieved message', str(message.payload))

def on_connect(client, userdata, flags, rc):
    #subscribe to the topic
    client.subscribe('TEST_TOPIC')

#create the mqtt object and connect to the broker
MQTT_BROKER = [broker-IP]
client = mqtt.Client('Client1')
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER)

#Do this continually so i can keep looking for messages published on this topic
client.loop_forever()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM