Last active
February 24, 2021 07:25
-
-
Save RutledgePaulV/b858726b919326a6c98f100d1751e799 to your computer and use it in GitHub Desktop.
doseq but with work broken out into thread pools of specified sizes
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
(defmacro workseq [bindings & body] | |
(let [bind (first (partition 2 bindings))] | |
(if (empty? bindings) | |
`(do ~@body nil) | |
`(let [right# ~(second bind) | |
value# (first right#) | |
workers# (second right#) | |
num-values# (count value#)] | |
(if (and (< 1 workers#) (< 1 num-values#)) | |
(let [thread-pool# (Executors/newFixedThreadPool workers#) | |
latch# (CountDownLatch. num-values#)] | |
(doseq [x# value#] | |
(let [~(first bind) | |
x# | |
thunk# | |
(bound-fn* | |
(^:once fn* [] | |
(try | |
(workseq ~(vec (drop 2 bindings)) ~@body) | |
(finally | |
(.countDown latch#)))))] | |
(.submit ^ThreadPoolExecutor thread-pool# ^Runnable thunk#))) | |
(.await latch#) | |
(.shutdownNow thread-pool#) | |
nil) | |
(doseq [x# value#] | |
(let [~(first bind) x#] | |
(workseq ~(vec (drop 2 bindings)) ~@body)))))))) | |
(comment | |
(workseq [x [(range 200) 12]] | |
(locking *out* | |
(println x)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment