我刚刚注意到 pipeline
家庭返回 channel
它似乎完全独立于管道的目的及其相关 channel 。
请注意,在以下示例中,您可以 >! / <!
来自 pipes
和 a> / b>
是分开的,它们是不相关的。
据我了解,pipeline
s 应该是空操作,并返回 nil
在设置副作用时 transduc
来自 a>
至 b>
.
那么,我错过了什么,为什么 pipeline
返回 channel
?
(def a> (chan))
(def b> (chan))
(def pipes (pipeline-blocking 4
b>
(map clojure.string/upper-case)
a>))
(go (>! pipes "hello world"))
(go (println "Pipes: " (<! pipes)))
(go (>! a> "apples are gooood"))
(go (println "B: " (<! b>)))
最佳答案
当没有更多要复制的元素时,您会返回一个关闭的 channel 。即在a>
之后已关闭,并且其中的所有元素都已大写并放置在 b>
上.您可以 <!
从生成的 channel 中找出流水线操作何时完成,如果你关心,或者你可以扔掉 channel 。你可能不应该写信给它。
这是许多异步操作的常见模式,并且确实经常隐式发生:每个 go
块返回一个 channel ,该 channel 在块完成时写入块的返回值,许多异步操作使用 go
块作为它们的返回值,因此您会自动获得这个“完成工作”的 channel 。
关于clojure - 为什么 `core.async/pipeline` 返回一个 channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40473827/