PubSub中的设备和设备记忆之间的差异是什么?

发布于 2025-01-29 06:34:56 字数 269 浏览 2 评论 0原文

如Pub-Sub的TOIT标准库文档中所写(请参阅 https://libs.toit。 io/pubsub/Library-Summary )有两个设备主题。其中之一是设备:*,另一个设备模式:*。

我找不到任何解释会有什么区别...有什么想法?

谢谢!

as written in the toit standard library documentation for pub-sub (see https://libs.toit.io/pubsub/library-summary) there are two device topics. One of them is device:* and the other one device-memory:*.

I couldn't find any explanation what the difference would be... Any idea?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身狗的梦 2025-02-05 06:34:56

设备内存版本不会将对象存储在Flash中。因此,它更有效,但是如果设备在订阅有机会阅读数据之前关闭设备,则可能会丢失数据。

例如:

// ---------- publish.toit -----------
import pubsub
main: pubsub.publish "device-memory:test" "test $Time.now"

// ---------- subscribe.toit ----------
import pubsub
main:
  print "running"
  sleep --ms=3_000
  pubsub.subscribe TOPIC:  | msg |
    print "received message: $msg.payload.to_string"

// ---------- publish.yaml ----------
name: device publisher
entrypoint: publish.toit
triggers:
  on_boot: true

// ---------- subscribe.yaml ----------
name: device subscriber
entrypoint: subscribe.toit
triggers:
  on_boot: true

pubsub:
  subscriptions:
  - "device-memory:test"

如果您安装这些程序,并重置设备,则应在日志中查看以下输出:

received message: test 2022-05-17T09:35:30Z    11:35:33    message
0                                              11:35:30    process stop
running                                        11:35:30    message
                                               11:35:30    process start
                                               11:35:30    process start

如果您在看到“运行”之后重置设备,但是在接收器有机会实际获取该设备之前数据,然后发布的消息将丢失。

但是,如果将主题切换到设备:test(在所有文件中),则将消息写入flash,重置设备不会丢失任何数据。

The device-memory version doesn't store the object in flash. As such it is more efficient, but it may lose data if the device is powered down before subscriptions have a chance to read the data.

For example:

// ---------- publish.toit -----------
import pubsub
main: pubsub.publish "device-memory:test" "test $Time.now"

// ---------- subscribe.toit ----------
import pubsub
main:
  print "running"
  sleep --ms=3_000
  pubsub.subscribe TOPIC:  | msg |
    print "received message: $msg.payload.to_string"

// ---------- publish.yaml ----------
name: device publisher
entrypoint: publish.toit
triggers:
  on_boot: true

// ---------- subscribe.yaml ----------
name: device subscriber
entrypoint: subscribe.toit
triggers:
  on_boot: true

pubsub:
  subscriptions:
  - "device-memory:test"

If you install these programs, and reset the device you should see the following output in your logs:

received message: test 2022-05-17T09:35:30Z    11:35:33    message
0                                              11:35:30    process stop
running                                        11:35:30    message
                                               11:35:30    process start
                                               11:35:30    process start

Now, if you reset the device just after you see the "running", but before the receiver has a chance to actually get the data, then the published message will be lost.

However, if you switch the topic to device:test (in all files), then the message is written to flash, and resetting the device won't lose any data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文