Python Paho MQTT:无法立即在函数中发布 [英] Python Paho MQTT: Unable to publish immediately in a function

查看:149
本文介绍了Python Paho MQTT:无法立即在函数中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个程序,该程序可以侦听特定主题,并在ESP8266发布新消息时对此作出反应.从ESP8266收到新消息时,我的程序将触发回调并执行一系列任务.我在回调函数中发布了两条消息,回到了Arduino正在侦听的主题.但是,仅在函数退出后才发布消息.

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 program will trigger the callback and perform a set of tasks. I am publishing two messages in my callback function back to the topic that the Arduino is listening. However, the messages are published only after the function exits.

提前感谢您的所有时间.

Thank you for all your time in advance.

我试图在回调函数中使用loop(1),超时时间为1秒.该程序将立即发布该消息,但似乎陷入了循环.有人可以给我一些指针,如何在我的回调函数中立即执行每个发布函数,而不是整个回调完成并返回到主loop_forever()时?

I have tried to use loop(1) with a timeout of 1 second inside the callback function. The program will publish the message immediately, but it seems to stuck in the loop. Will someone be able to give me some pointers how can I execute each publish function immediately in my callback function, instead of when the whole callback completes and return to the main loop_forever()?

import paho.mqtt.client as mqtt
import subprocess
import time

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("ESP8266")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    client.publish("cooking", '4')
    client.loop(1)
    print("Busy status published back to ESP8266")
    time.sleep(5)
    print("Starting playback.")
    client.publish("cooking", '3')
    client.loop(1)
    print("Free status published published back to ESP8266")
    time.sleep(5)
    print("End of playback.")


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("192.168.1.9", 1883, 60)
#client.loop_start()

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

推荐答案

您不能执行此操作,因为您已经处于消息发布循环(即所谓的on_message函数)中.这样会将传出的消息排队,由循环的下一个迭代处理,这就是为什么一旦on_message返回就发送它们的原因.

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.

当您调用loop方法时,它会挂起,因为循环已经在运行.

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

无论如何,您都不应在on_message回调中进行阻塞(睡眠)调用,如果您需要做一些耗时的事情,请启动另一个线程来执行这些操作.这样,您就可以释放网络循环,以便在发布后立即处理它们.

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.

这篇关于Python Paho MQTT:无法立即在函数中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆