多播与 core.async 广播
mult vs broadcast in core.async
我在玩 core.async 并发现使用起来非常有趣。但是我无法理解 mult 和 broadcast 的不同用例。它们是否都需要,或者一个将被另一个取代?
到目前为止,我发现的唯一区别是使用 mult 更容易点击和取消标记。不确定如何取消订阅广播,这是唯一的区别吗?
下面我有一个例子展示了如何使用这两种方法解决问题。
;; Using mult with tap
(def in (chan))
(def multiple (mult in))
(def out-1 (chan))
(tap multiple out-1)
(def out-2 (chan))
(tap multiple out-2)
(go (>! in "PutIN"))
(go (prn "From out-1: " (<! out-1)))
(go (prn "From out-2: " (<! out-2)))
//
;; Using broadcast
(def bout-1 (chan))
(def bout-2 (chan))
(def broadcast-in (broadcast bout-1 bout-2))
(go (>! broadcast-in "PutINBroadcast"))
(go (prn "From bout-1: " (<! bout-1)))
(go (prn "From bout-2: " (<! bout-2)))
这是对带有 broadcast
的 clojure.core.async.lab
命名空间的注释。
core.async HIGHLY EXPERIMENTAL feature exploration
Caveats:
Everything defined in this namespace is experimental, and subject to change or deletion without warning.
Many features provided by this namespace are highly coupled to implementation details of core.async. Potential features which operate
at higher levels of abstraction are suitable for inclusion in the
examples.
Features provided by this namespace MAY be promoted to clojure.core.async at a later point in time, but there is no guarantee
any of them will.
现在很长一段时间都没有人研究过它,它从未被移植到 ClojureScript。我预计它会在不久的将来从 core.async 中删除。 mult
是后期的发展。
我在玩 core.async 并发现使用起来非常有趣。但是我无法理解 mult 和 broadcast 的不同用例。它们是否都需要,或者一个将被另一个取代? 到目前为止,我发现的唯一区别是使用 mult 更容易点击和取消标记。不确定如何取消订阅广播,这是唯一的区别吗?
下面我有一个例子展示了如何使用这两种方法解决问题。
;; Using mult with tap
(def in (chan))
(def multiple (mult in))
(def out-1 (chan))
(tap multiple out-1)
(def out-2 (chan))
(tap multiple out-2)
(go (>! in "PutIN"))
(go (prn "From out-1: " (<! out-1)))
(go (prn "From out-2: " (<! out-2)))
//
;; Using broadcast
(def bout-1 (chan))
(def bout-2 (chan))
(def broadcast-in (broadcast bout-1 bout-2))
(go (>! broadcast-in "PutINBroadcast"))
(go (prn "From bout-1: " (<! bout-1)))
(go (prn "From bout-2: " (<! bout-2)))
这是对带有 broadcast
的 clojure.core.async.lab
命名空间的注释。
core.async HIGHLY EXPERIMENTAL feature exploration
Caveats:
Everything defined in this namespace is experimental, and subject to change or deletion without warning.
Many features provided by this namespace are highly coupled to implementation details of core.async. Potential features which operate at higher levels of abstraction are suitable for inclusion in the examples.
Features provided by this namespace MAY be promoted to clojure.core.async at a later point in time, but there is no guarantee any of them will.
现在很长一段时间都没有人研究过它,它从未被移植到 ClojureScript。我预计它会在不久的将来从 core.async 中删除。 mult
是后期的发展。