我正在尝试运行一个MQTT客户端,该客户端同时接收和发布消息。我正在尝试做的是在收到消息时停止while循环,该循环负责发布。但是,对于while循环,运行永远不会为False,并且循环永远不会中断。我该怎么办?我只想在收到消息时打破循环。这是我的代码:
running = True
def on_message(client, userdata, msg):
print(msg)
running = False
print(running) # it becomes False here
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, 1883, 60)
client.loop_start() # used to receive on_message events while the while loop is true
while running:
#publish and do something until running is False