Created
October 14, 2019 13:41
-
-
Save agrison/31defbcd4a3cd91837d77ebdc8bab613 to your computer and use it in GitHub Desktop.
runp! runs a given function on a given collection in parallel.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn runp! | |
"Runs the function `f` in parallel on the given collection `coll`. | |
It will use (by default) the same numbers of threads as there are cores available on the system. | |
You can set a specific number of threads using `:threads` waits maximum `:timeout` milliseconds." | |
([f coll {:keys [threads timeout] :or {threads (.availableProcessors (Runtime/getRuntime))}}] | |
(let [pool (java.util.concurrent.Executors/newFixedThreadPool threads) | |
tasks (map (fn [e] (fn [] (f e))) coll)] | |
(try | |
(doseq [future (.invokeAll pool tasks)] | |
(.get future)) | |
(catch InterruptedException e | |
(.shutdownNow pool)) | |
(finally | |
(.shutdown pool) | |
(when timeout | |
(when-not (.awaitTermination pool timeout java.util.concurrent.TimeUnit/MILLISECONDS) | |
(.shutdownNow pool)))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn runp! | |
"Runs the function `f` in parallel on the given collection `coll`. | |
It will use (by default) the same numbers of threads as there are cores available on the system. | |
You can set a specific number of threads using `:threads`." | |
([f coll] | |
(runp! f coll (.availableProcessors (Runtime/getRuntime)))) | |
([f coll threads] | |
(let [pool (java.util.concurrent.Executors/newFixedThreadPool threads) | |
tasks (map (fn [e] (fn [] (f e))) coll)] | |
(try | |
(doseq [future (.invokeAll pool tasks)] | |
(.get future)) | |
(finally | |
(.shutdown pool)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment