使用 class 从 on_log 回调中寻找 MQTT PUBACK 失败,但它在平面脚本中有效
Looking for MQTT PUBACK from on_log callback using within a class fails, but it works in a flat script
第一个脚本有效,这意味着调用回调并结束打印 puback: True
我使用 class A
完成工作的第二个脚本不起作用。不调用回调,以a.puback: False
结束
我不确定我的问题是否是回调无法以这种方式工作,在这种情况下,我如何才能让我的 class 使用这些 Paho MQTT 回调?或者如果它更微妙。
作品:
def on_log_puback(client, userdata, level, buf):
global puback
if 'PUBACK' in buf:
puback = True
print "PUBACK!"
def on_connect(client, userdata, flags, rc):
print "Connect code: ", rc
def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc
def on_message(client, userdata, msg):
print " Message: ", str(msg.payload.decode("utf-8", "ignore"))
def stop():
print ("stopping loop")
client.loop_stop()
print "disconnecting"
client.disconnect()
import paho.mqtt.client as mqtt
import time
client = mqtt.Client("Luke, I am your client")
mqtt.Client.connected_flag = False
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_log = on_log_puback
client.on_message = on_message
status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)
print "loop_start"
client.loop_start()
time.sleep(1)
sret = client.subscribe("test_topic")
print "subscribe returns sret: ", sret
time.sleep(2)
# initialize global
puback = False
# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)
print "puback: ", puback
stop()
无效:
import paho.mqtt.client as mqtt
import time
class A(object):
def __init__(self):
client = mqtt.Client("Luke, I am your client")
self.client = client
mqtt.Client.connected_flag = False
client.on_connect = self.on_connect
client.on_disconnect = self.on_disconnect
client.on_log = self.on_log_puback
client.on_message = self.on_message
status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)
print "loop_start"
client.loop_start()
time.sleep(1)
sret = client.subscribe("test_topic")
print "subscribe returns: ", sret
time.sleep(2)
# initialize global
puback = False
# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)
self.puback = puback
def on_log_puback(client, userdata, level, buf):
global puback
if 'PUBACK' in buf:
puback = True
print "PUBACK!"
def on_connect(client, userdata, flags, rc):
print "Connect code: ", rc
def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc
def on_message(client, userdata, msg):
print " Message: ", str(msg.payload.decode("utf-8", "ignore"))
def stop(self):
print ("stopping loop")
self.client.loop_stop()
print "disconnecting"
self.client.disconnect()
a = A()
time.sleep(2)
print 'a.puback: ', a.puback
a.stop()
找到了。当我将回调移动到 class 时,例如on_log_puback(self,...)
我只是忘记将 self
添加到参数的开头。这样,它现在可以很好地工作了。
第一个脚本有效,这意味着调用回调并结束打印 puback: True
我使用 class A
完成工作的第二个脚本不起作用。不调用回调,以a.puback: False
我不确定我的问题是否是回调无法以这种方式工作,在这种情况下,我如何才能让我的 class 使用这些 Paho MQTT 回调?或者如果它更微妙。
作品:
def on_log_puback(client, userdata, level, buf):
global puback
if 'PUBACK' in buf:
puback = True
print "PUBACK!"
def on_connect(client, userdata, flags, rc):
print "Connect code: ", rc
def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc
def on_message(client, userdata, msg):
print " Message: ", str(msg.payload.decode("utf-8", "ignore"))
def stop():
print ("stopping loop")
client.loop_stop()
print "disconnecting"
client.disconnect()
import paho.mqtt.client as mqtt
import time
client = mqtt.Client("Luke, I am your client")
mqtt.Client.connected_flag = False
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_log = on_log_puback
client.on_message = on_message
status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)
print "loop_start"
client.loop_start()
time.sleep(1)
sret = client.subscribe("test_topic")
print "subscribe returns sret: ", sret
time.sleep(2)
# initialize global
puback = False
# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)
print "puback: ", puback
stop()
无效:
import paho.mqtt.client as mqtt
import time
class A(object):
def __init__(self):
client = mqtt.Client("Luke, I am your client")
self.client = client
mqtt.Client.connected_flag = False
client.on_connect = self.on_connect
client.on_disconnect = self.on_disconnect
client.on_log = self.on_log_puback
client.on_message = self.on_message
status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)
print "loop_start"
client.loop_start()
time.sleep(1)
sret = client.subscribe("test_topic")
print "subscribe returns: ", sret
time.sleep(2)
# initialize global
puback = False
# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)
self.puback = puback
def on_log_puback(client, userdata, level, buf):
global puback
if 'PUBACK' in buf:
puback = True
print "PUBACK!"
def on_connect(client, userdata, flags, rc):
print "Connect code: ", rc
def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc
def on_message(client, userdata, msg):
print " Message: ", str(msg.payload.decode("utf-8", "ignore"))
def stop(self):
print ("stopping loop")
self.client.loop_stop()
print "disconnecting"
self.client.disconnect()
a = A()
time.sleep(2)
print 'a.puback: ', a.puback
a.stop()
找到了。当我将回调移动到 class 时,例如on_log_puback(self,...)
我只是忘记将 self
添加到参数的开头。这样,它现在可以很好地工作了。