【发布时间】:2015-09-24 22:05:32
【问题描述】:
我有两个单向core.async 频道:
- 频道输出只能
put! - 频道只能
take!
因为这是 ClojureScript,所以阻塞操作不可用。我想从这两个(输入和输出)通道中创建一个双向(输入输出)通道。
(def in (async/chan))
(def out (async/chan))
(def in-out (io-chan in out)) ;; io or whatever the solution is
(async/put! in "test")
(async/take! ch (fn [x] (println x))) ;; should print "test"
(async/put! ch) ;; put into ch is equivalent to putting into `out`
我尝试了类似以下的方法(不起作用):
(defn io-chan [in-ch out-ch]
(let [io (chan)]
(go-loop []
(>! out-ch (<! io ))
(>! io (<! in-ch))
(recur))
io))
架构可能会有所帮助:
out in-out
---------------> (unused)
<--------------- <---------------
in
----------------> ---------------->
<---------------- (unused)
另外,关闭双向通道应该关闭两个底层通道。
有可能吗?
【问题讨论】:
-
对不起,我没有得到你的用例,也许你可以添加一个你想要的代码示例?
-
@ValentinWaeselynck 我编辑了我的问题,希望现在有意义。
-
仍然不确定我是否明白:/您在描述中提到了 6 个频道名称(A、B、输入、输出、io、ch)。如果你统一这个会有所帮助
-
@ValentinWaeselynck 是的,正确,已更正:)
标签: clojure clojurescript core.async