在clojure.core.async中添加延​​迟

icy*_*com 5 asynchronous clojure

当使用clojure.core.async时,有没有办法让一个频道等待第一个项目放在上面,然后等待一小段时间,然后获取当前频道上的所有项目(可能已经到达了等待)并让所有这些都没有阻塞?

即有一种方法可以实现get-available-items:

(defn delayer [ch ch2]
  (go (loop []
        (when-let [v (<! ch)]
          (<! (timeout 500))
          (let [vs (get-available-items ch)
                items (cons v vs)]
            (>! ch2 items))
          (recur)))))
Run Code Online (Sandbox Code Playgroud)

基本上,像Java中的BlockingQueue.drain.

Art*_*ldt 6

有计划通过频道提供此功能,但现在您可以通过以下方式检查频道上是否存在某些内容:

(alts!! [my-chan] :default :nothing-immediately-in-chan)
Run Code Online (Sandbox Code Playgroud)

通过迭代,您可以在不阻塞的情况下排空通道.

PS:额外感谢tbaldridge和julianlevis在#clojure帮助这个