Fork me on GitHub
#core-async
<
2017-06-17
>
tianshu02:06:47

should I explict close! the chan when I dont need it?

noisesmith03:06:15

that code would go into a very fast loop if so (if it never parks in the ...) because you are not checking the value of <! and it returns nil instantly for a closed chan

noisesmith03:06:28

usually you would want (try (when-some [next-item (<! c)] ... (recur) (catch ...)) to prevent the hot loop

joshjones04:06:35

@doglooksgood If you close it, this will ensure that any blocked or parked takes via <!! and <! will return nil (note that even after a channel is close!d, a take will return data in the buffer, until it is empty). For example, if you have a go loop parked on a take, then when you close the channel, it will receive nil and depending on how you've coded it (i.e., check the value you get from the chan, and if it's nil, do not recur), the code in the go block will no longer be executing. Likewise, puts via >!! and >! will return false instead of blocking/parking.

plins11:06:21

hello everyone

(defn five-seconds-loop
  [poison in-chan]
  (async/go-loop []
    (let [[data chosen] (async/alts! [poison in-chan])]
      (when (not= chosen poison)
        (if (some? data)
          (do-stuff)
        (async/<! (async/timeout 5000))
        (recur))))))
id like this whole loop code block to take at 5 seconds at maximum to execute including the time do-stuff and reading from in-chan takes if the code isnt able to execute in 5 seconds the loop should start again is it possible?

noisesmith13:06:44

@plins if data is ever nil you should stop calling recur at that point

noisesmith13:06:11

(that is, if it came from in-chan and is nil)

noisesmith14:06:58

if it should start every 5 seconds, why not (go-loop [] (do-it) (<! (timeout 5000)) (recur)) where do-it is a call to go with your let block without the recur