doseq in core.async go macro not working

426 Views Asked by At

I'm new to core.async and I'm wondering why the following does NOT work (no output at all):

(def jackie (chan 2))
(go (loop [food (<! jackie)]
  (if food
    (do 
      (println "Some" food "is what I was waiting for.")
      (Thread/sleep 1000)
      (recur (<! jackie))))))

(go (doseq [food ["carrots" "peas"]]
  (println "deliver" food)
  (>! jackie food)
  (Thread/sleep 1000)))

... while this DOES work:

(def jackie (chan 2))
(go (loop [food (<! jackie)]
  (if food
    (do 
      (println "Some" food "is what I was waiting for.")
      (Thread/sleep 1000)
      (recur (<! jackie))))))

(doseq [food ["carrots" "peas"]]
  (println "deliver" food)
  (>!! jackie food)
  (Thread/sleep 1000))

The only difference here is the missing go block around the last doseq.

I found the combined go and doseq in an example in this blog post, but it doesn't work for me. Also doing it the other way round and nesting the go inside the doseq like in this question does not work for me.

1

There are 1 best solutions below

1
On

>!! will block the current thread if the target channel/buffer is full. To have the current go block park instead, use the single-bang variant >!