gpt4 book ai didi

dart:如何实现保留传入项目顺序的流连接?

转载 作者:行者123 更新时间:2023-12-02 02:01:33 25 4
gpt4 key购买 nike

我正在尝试实现一个连接运算符,该运算符采用流列表并输出单个流。问题是输出顺序与输入顺序不同。 StreamController.add 是异步的,所以如果我这样做

sc1.add(1)
sc2.add(2)
sc2.add(3)
sc2.add(4)
sc1.add(5)
...

调用各个流 ondata 回调的顺序类似于 1、2、5、3、4,这基本上是 sc1 和 sc2 的交错。这种顺序在执行过程中非常一致,这让我相信该实现是在单个线程上进行循环调度。 (这是在 Dart 虚拟机上)

如果在我的连接实现中调用 ondata 回调时顺序已经被打乱,我将无法正确实现此连接。有没有人对如何实现这个有好的想法?

最佳答案

我相信您使用的是最新版本的 SDK。

sync 标志是从 v0.5.11 开始为 SteamController 构造函数引入的,它控制此行为(参见文档 herehere)。默认情况下它被设置为 false,这意味着监听器被异步调用,即在 add 调用完成后的某个时间:

If sync is false, no guarantees are given with regard to when multiple listeners get the events, except that each listener will get all events in the correct order. If two events are sent on an async controller with two listeners, one of the listeners may get both events before the other listener gets any

如果您没有 Controller ,那么我不确定您能做些什么。

关于dart:如何实现保留传入项目顺序的流连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com