简体   繁体   English

paho-mqtt 订阅一次性消息

[英]Paho-mqtt subscribe one-time message

Is there an elegant way to pull one message off the broker without having to:有没有一种优雅的方法可以从代理中提取一条消息而不必:

  1. subscribe订阅
  2. create an on_message()创建一个 on_message()
  3. receive the message接收消息
  4. unsubscribe退订

I ask because we are using a json message which has multiple fields.我问是因为我们使用的是具有多个字段的 json 消息。 When new data comes in I want to ONLY update that particular field in the json message but not remove the rest of the data.当新数据进入时,我只想更新 json 消息中的特定字段,但不删除数据的 rest。 Since we have a TON of these json topics, we don't really want to keep all of them in program memory (also in case the program has to be relaunched).由于我们有大量的 json 主题,我们真的不想将它们都保留在程序 memory 中(也以防程序必须重新启动)。 On top of that, this program could be running for months without supervision.最重要的是,该程序可以在没有监督的情况下运行数月。

So ideally, I'd like to post the json message to an ID'd topic with the retain flag set to True.所以理想情况下,我想将 json 消息发布到一个 ID 主题,并将保留标志设置为 True。 Then when new data comes in for that ID, I do a pull of the info on that topic, update that particular field in the json message and repost to the same topic.然后,当该 ID 的新数据进入时,我会提取有关该主题的信息,更新 json 消息中的特定字段并重新发布到同一主题。

I can post example code but I'm hoping there is a simple function that I am unaware of.我可以发布示例代码,但我希望有一个我不知道的简单 function。

Thanks, in advance, for any suggestions.在此先感谢您的任何建议。

The Paho Python client comes with a set of help classes that do this single shot type of pattern for you. Paho Python 客户端带有一组帮助类,可以为您执行这种单次模式类型。

Doc here文档在这里

eg the following connects to a broker, subscribes to a topic and returns on receipt of the first message on that topic.例如,以下连接到代理,订阅一个主题并在收到关于该主题的第一条消息时返回。

import paho.mqtt.subscribe as subscribe

msg = subscribe.simple("paho/test/simple", hostname="mqtt.eclipse.org")
print("%s %s" % (msg.topic, msg.payload))

And the matching publish call:以及匹配的发布调用:

import paho.mqtt.publish as publish

publish.single("paho/test/single", "payload", hostname="mqtt.eclipse.org")

I don't think that is possible.我不认为这是可能的。 You say "When new data comes in..." That's exacty why you need to subscribe and use the callback function.你说“当新数据进来时......”这正是你需要订阅和使用回调 function 的原因。 That's basically a "pull when something is actually there".这基本上是“当某物实际存在时的拉动”。

Just to get an idea of how it should work: you are sending that json message via MQTT, right?只是为了了解它应该如何工作:您正在通过 MQTT 发送 json 消息,对吗? And you are re-sending it when it changes?当它改变时你会重新发送它吗?

But you don't have to keep them all in the RAM.但是您不必将它们全部保存在 RAM 中。 You could use a retained message in combination with a fixed topic (not ID'ed) and send the ID in the message.您可以将保留消息与固定主题(未标识)结合使用,并在消息中发送标识。

If you use retained messages with ID'ed topics, that might fill the memory.如果您使用带有 ID 主题的保留消息,则可能会填满 memory。

What does the ID stand for?身份证代表什么? A uniqie number?独一无二的号码? Something like a timestamp?时间戳之类的东西? A hash? hash? The sender?发件人?

I think you can solve that problem by clearly separating your things, eg say in data and message , where data is something you maintain in Python (eg a database or something in RAM) and message is something that you acually send / receive via MQTT.我认为你可以通过清楚地分离你的东西来解决这个问题,例如在datamessage中说,其中data是你在 Python 中维护的东西(例如数据库或 RAM 中的东西),而message是你通过 MQTT 实际发送/接收的东西。

Then you can add / send / update data depending on what is received in MQTT and you don't have to send / update the complete set.然后,您可以根据在 MQTT 中接收到的内容添加/发送/更新data ,而不必发送/更新完整的数据集。

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

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