This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-26
Channels
- # arachne (80)
- # beginners (76)
- # boot (16)
- # cider (66)
- # cljs-dev (62)
- # cljsjs (1)
- # clojure (106)
- # clojure-dev (5)
- # clojure-greece (2)
- # clojure-italy (9)
- # clojure-russia (1)
- # clojure-spec (61)
- # clojure-uk (130)
- # clojurescript (21)
- # core-async (9)
- # cursive (3)
- # datomic (37)
- # events (41)
- # figwheel (31)
- # fulcro (27)
- # hoplon (1)
- # jobs (2)
- # lumo (11)
- # off-topic (155)
- # re-frame (71)
- # reagent (27)
- # ring-swagger (3)
- # shadow-cljs (132)
- # spacemacs (5)
- # specter (1)
- # sql (37)
- # test-check (10)
- # uncomplicate (5)
- # unrepl (2)
- # yada (3)
For a channel c
, I’ve some code like:
(let [latch (atom (count urls))]
(doseq [url urls]
(thread
(let [resp (http/get url ...)]
(when (some-condition resp)
(>!! c (do-something resp))))
(when (zero? (swap! latch dec))
(close! c)))))
Is there a more idiomatic way to do this? Particularly the bit about closing the channel. The latch method is the best way I can think.@xiongtx take a look at async/pipeline-blocking
, it maintains ordering (might not be what you want.
But that's the general idea, create a pipeline, flow data through it
Hmm…is pipeline-blocking
or pipeline-async
more appropriate in this case? I don’t care about the order of the responses on the output chan.
Also:
- Does pipeline-*
’s parallelism have the same caveat r.e. blocking ops as go
blocks? https://eli.thegreenplace.net/2017/clojure-concurrency-and-blocking-with-coreasync#blocking-i-o
- I’d assume pipeline-*
spins up n
threads, where n
is the degree of parallelism. Then it’d be bad to block any of those n
threads, yes?
- Does the channel parameter of af
in pipeline-async
correspond to the to
chan of pipeline-async
itself? I assume so, but the docs aren’t clear on that.
From the src of pipeline*
it seems that :blocking
uses thread
, while :async
and :compute
use go
blocks, so I guess pipeline-blocking
is the right thing to use.
Yes, pipeline-blocking is designed for you to potentially block
Is there a a more idiomatic way to "get all values off a channel until the channel is closed" than:
(loop []
(when-let [v (<!! c)]
(do-something v)
(recur)))
I suppose I could use pipeline-*
again, or map
, but in this case I don't care to put the result of (do-something v)
onto another channel. So this'd be more like doseq
.You can reduce off the channel