For the record, I ended up using core.async/merge instead, but I modified 
it to also return the channel for each value, as [v c] -- similarly to alts!

On Saturday, 26 September 2015 02:29:17 UTC+1, Stathis Sideris wrote:
>
> Hello,
>
> I'm trying to process messages from 2 core.async channels at the same time 
> using alts!! but I'm running into problems when one of the channels closes 
> early. Specifically I have this function:
>
> (defn >print [a b]
>   (loop [limit 20] ;;limit to prevent infinite loop demonstrated below
>     (let [[v c] (alts!! [a b] :default ::done)]
>       (if (and (not= c :default) (> limit 0))
>         (do
>           (if (= c a)
>             (println "a:" v)
>             (println "b:" v))
>           (recur (dec limit))))))
>   nil)
>
> If I run this, it works as expected:
>
> (let [a (chan 10)
>       b (chan 10)]
>     (>!! a 1)
>     (>!! a 1)
>     (>!! a 1)
>     (>!! a 1)
>     (>!! b 2)
>     (>!! b 2)
>     (>!! b 2)
>     (>!! b 2)
>     (>!! b 2)
>     (>print a b))
>
> Output:
>
> b: 2
> a: 1
> a: 1
> b: 2
> b: 2
> a: 1
> b: 2
> b: 2
> a: 1
>
> If I close "a" early...
>
> (let [a (chan 10)
>       b (chan 10)]
>     (>!! a 1)
>     (close! a)
>     (>!! a 1)
>     (>!! a 1)
>     (>!! a 1)
>     (>!! b 2)
>     (>!! b 2)
>     (>!! b 2)
>     (>!! b 2)
>     (>!! b 2)
>     (>print a b))
>
> This is what I get:
>
> a: 1
> b: 2
> b: 2
> b: 2
> a: nil
> a: nil
> b: 2
> b: 2
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
> a: nil
>
> This code would continue running forever if it wasn't for the "limit" 
> condition above. Am I using alts!! in a manner it's not meant to be used?
>
> Thanks,
>
> Stathis
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to