0
我想問一下,如果有使用msg.payload
全球,而不只是在回調函數的方式。如何將msg.payload全局用於MQTT Python客戶端?
貝婁是基本的回調函數:
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
# I usually then specify my steps here, but how can I use the variable outside?
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
例如,我想要做這樣的事情:
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
return msg.payload
global_data= on_message
# do whatever I want with the data. I already tried to do this but it didn't work because
# the client loops forever using client.loop_forever()
你是什麼意思?該消息只存在於回調中。如果你在其他地方需要它,你需要在那裏傳遞它。 –
是的,但我如何通過它? –
與其他函數參數相同。我不確定你在問什麼。 –