Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute the same function multiple times in parallel in Clojure?

I have a function that I'd like to run multiple times, generating a list of the results:

(take 10 (repeatedly #(myfunc)))

I realized I could run them in parallel with pmap:

(pmap (fn [_] (myfunc)) (range 10))

But it is a bit untidy. Is there a standard function that lets me do this Something like:

(prun 10 #(myfunc))

?


2 Answers

You may also be interested in The Claypoole library for managing threadpools and parallel processing. Look at their version of pmap and pfor.

like image 158
Alan Thompson Avatar answered Apr 17 '26 03:04

Alan Thompson


I don't think there's an existing function, but using pcalls rather than pmap seems a little closer to what you want:

(defn prun [n f]
  (apply pcalls (repeat n f)))

You don't need to wrap myfunc with #() in the call torepeatedly, btw, nor calling prun as defined above:

(prun 10 myfunc)

You may find pvalues useful as well.

like image 21
Mars Avatar answered Apr 17 '26 01:04

Mars



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!