0

I am trying to run an MQTT client that receives and publishes messages at the same time. What I am trying to do is to stop the while loop responsible for publishing when I receive a message. But running never becomes False for the while loop and the loop is never broken. What should I do? I just want to break the loop when a message is received. Here's my code:

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
Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41

0 Answers0