This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-17
Channels
- # beginners (10)
- # boot (9)
- # cider (1)
- # cljs-dev (191)
- # clojure (77)
- # clojure-russia (4)
- # clojure-serbia (10)
- # clojure-spec (19)
- # clojure-uk (4)
- # clojurescript (16)
- # core-async (9)
- # cursive (1)
- # data-science (1)
- # datomic (3)
- # emacs (42)
- # graphql (2)
- # hoplon (38)
- # jobs (1)
- # jobs-discuss (18)
- # luminus (3)
- # lumo (20)
- # off-topic (9)
- # om (51)
- # parinfer (3)
- # pedestal (8)
- # re-frame (4)
- # reagent (7)
- # rum (9)
- # sql (9)
- # untangled (81)
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
usually you would want (try (when-some [next-item (<! c)] ... (recur) (catch ...))
to prevent the hot loop
@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.
thanks very much @joshjones @noisesmith
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?@plins if data is ever nil you should stop calling recur at that point
(that is, if it came from in-chan and is nil)
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