I'm trying to convert a channel into a seq. This is what I'm using currently:
(defn chan->seq [ch]
(when [v (<!! ch)]
(lazy-seq (cons v (chan->seq ch)))))
It works but it uses a blocking read, so the current thread will be blocked until the channel is closed, and you can't have many calls of this function active at the same time. Is it possible to have a non-blocking version of this function?
This example demonstrates this issue:
(doseq [_ (range 20)]
(go (chan->seq (chan 10))))
(go (println :OK))
The last statement won't print anything since
chan->seq calls are squatting on all the threads in the threadpool. It works if 20 is changed to 5 (in a new repl session).