Fork me on GitHub
#core-async
<
2018-01-26
>
xiongtx03:01:07

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.

tbaldridge04:01:45

@xiongtx take a look at async/pipeline-blocking, it maintains ordering (might not be what you want.

tbaldridge04:01:55

But that's the general idea, create a pipeline, flow data through it

xiongtx06:01:53

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.

xiongtx07:01:17

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.

Alex Miller (Clojure team)13:01:34

Yes, pipeline-blocking is designed for you to potentially block

xiongtx07:01:43

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.

Alex Miller (Clojure team)13:01:26

You can reduce off the channel

xiongtx18:01:14

Ah, yes, saw reduce. :thumbsup: