Python Paho MQTT: Unable to publish immediately in a function

后端 未结 1 1901
春和景丽
春和景丽 2021-01-21 20:15

I am implementing a program that listen to a specific topic and react to it when a new message is published by my ESP8266. When a new message is received from ESP8266, my progra

相关标签:
1条回答
  • 2021-01-21 21:03

    You can't do this, you are already in the message handling loop (that's what called the on_message function) at the point you call publish. This will queue the outgoing messages to be handled by the next iteration of the loop, that's why they are sent once on_message returns.

    It hangs when you call the loop method because the loop is already running.

    You should not be making blocking (sleep) calls in the on_message callback anyway, if you need to do thing that take time, start up a second thread to do these. By doing this you free up the network loop to handle the outgoing publishes as soon as they are made.

    0 讨论(0)
提交回复
热议问题