Fork me on GitHub
#core-async
<
2017-07-25
>
twashing19:07:30

I’m just missing something simple here. Do we know why this code only works when the timeout channel wins?

(def c (chan))

(let [begin (System/currentTimeMillis)
        [v ch] (async/alts!! [c (async/timeout 5000)])]

    (println "Message: " v)
    (println "Timer: " (- (System/currentTimeMillis) begin)))

;; Should call if we want the non-timeout channel to win... But the call from async/alts!! just hangs
(>!! c "Hello World")

tbaldridge19:07:55

Yes, it will just block because nothing has been put into the channel yet.

tbaldridge19:07:01

And your let block is running on the repl thread

tbaldridge19:07:18

You need to wrap the let block in something like async/thread

twashing19:07:44

@tbaldridge Thanks, yeah just figured it out. I put the production in a go block.

(defn foo
    [value c]
    (go (>! c value)))

  (let [c1 (chan)]
    (foo "Hello" c1)
    (let [[v channel] (alts!! [c1 (timeout 1000)])]
      (if v
        (println "Value: " v)
        (println "Timed out!"))))

tbaldridge19:07:23

@twashing sure, but dont use alts!! in that case, use alts!. If you use the !! you can quite easily starve the go thread pool.

twashing19:07:02

Hmm, ok cool. I’m getting the gist of it now. 👍:skin-tone-5: