简体   繁体   English

使用 Paho-MQTT 处理来自 MQTT stream 的数据

[英]Processing Data from an MQTT stream using Paho-MQTT

I have a microcontroller that is streaming data to a MQTT broker and also a python script using the Paho-MQTT package to subscribe to the topic that my microcontroller is publishing to.我有一个微控制器正在将数据流式传输到 MQTT 代理,还有一个 python 脚本使用 Paho-MQTT package 来订阅我的微控制器正在发布的主题。 In all of the examples that I see with Paho-MQTT the script is constantly looping the client as seen below:在我看到的所有使用 Paho-MQTT 的示例中,脚本不断循环客户端,如下所示:

def subscribe(client: mqtt_client):
    def on_message(client, userdata, msg):
        messageQueue = msg.payload.decode()
        print(messageQueue)

    client.subscribe(topic)
    client.on_message = on_message


def run():
    client = connect_mqtt()
    subscribe(client)
    client.loop_forever()

run()

The problem that I'm running into is that I need to actually process the data that is being sent and since loop_forever is a blocking process, I can't ever break through to do anything with the data.我遇到的问题是我需要实际处理正在发送的数据,并且由于 loop_forever 是一个阻塞进程,我无法突破以对数据执行任何操作。 I've looked into multi-threading since it seemed like that would allow me to run both the MQTT client and the processing simultaneously, but it seems like there is no way to transfer variables between threads.我研究了多线程,因为它似乎允许我同时运行 MQTT 客户端和处理,但似乎没有办法在线程之间传输变量。 Is there something that I'm doing wrong, or how should I manage this?我做错了什么,或者我应该如何处理?

Thanks in Advance!提前致谢!

I've looked into multi-threading since it seemed like that would allow me to run both the MQTT client and the processing simultaneously, but it seems like there is no way to transfer variables between threads.我研究了多线程,因为它似乎允许我同时运行 MQTT 客户端和处理,但似乎没有办法在线程之间传输变量。 I've also tried stopping the loop to process and then restarting the loop but that also hasn't worked in that it never gets to the parts of my code where I process the data, it just forever is listening.我也试过停止循环进行处理,然后重新启动循环,但这也没有奏效,因为它永远不会到达我处理数据的代码部分,它只是永远在听。

You may want to take a look at the " Network loop " section of the documentation, which shows you that calling client.loop_forever() isn't your only option.您可能想看一下文档的“网络循环”部分,它告诉您调用client.loop_forever()不是您唯一的选择。

The problem that I'm running into is that I need to actually process the data that is being sent and since loop_forever is a blocking process, I can't ever break through to do anything with the data.我遇到的问题是我需要实际处理正在发送的数据,并且由于loop_forever是一个阻塞进程,我无法突破对数据做任何事情。

You can process the data in your on_message function;你可以处理你的on_message function中的数据; that code gets called for every message received.收到的每条消息都会调用该代码。 Instead of simply calling print(messageQueue) , you can perform whatever processing is necessary:您可以执行任何必要的处理,而不是简单地调用print(messageQueue)

def subscribe(client: mqtt_client):
    def on_message(client, userdata, msg):
        messageQueue = msg.payload.decode()
        print("PROCESS THE MESSAGE HERE")

    client.subscribe(topic)
    client.on_message = on_message

but it seems like there is no way to transfer variables between threads但似乎没有办法在线程之间传递变量

You don't transfer variables between threads;您不在线程之间传输变量 you transfer data between threads.您在线程之间传输数据 If you use the loop_start method to start the client loop in a background thread, you can use a Queue to pass messages from the client thread to your main thread for processing.如果使用loop_start方法在后台线程中启动客户端循环,则可以使用Queue将消息从客户端线程传递到主线程进行处理。 That might look like:这可能看起来像:

from queue import Queue
import paho.mqtt.client as mqtt  # import the client1


def run_client(q):
    def on_message(client, userdata, msg):
        msg = msg.payload.decode()
        q.put(msg)

    client = mqtt.Client("example")  # create new instance
    client.connect("test.mosquitto.org")  # connect to broker
    client.subscribe("TELEMTRY")
    client.on_message = on_message
    client.loop_start()

def run():
    q = Queue()
    run_client(q)

    while True:
        msg = q.get()

        # Process message here
        print("processing:", msg)

if __name__ == "__main__":
    run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM