I have two different function on two very large sets of data that need to be processed, in the end coming down to two Boolean values. those values need to then be anded together for the final result. my question is what is the best way to create threads so the two long functions can run at the same time. my thoughts were something like,
(def f (future longProcessOne(data_one)))
(def g (future longProcessTwo(data_two)))
(and @f @g)
but I was looking for input on a better way to go about this.
Your approach is fairly normal Clojure code. One other choice is to use promises or if you need more complex processing you could consider using something like lamina or if you feel like living on the bleeding edge you could try core.async:
(ns async-example.core
(:require [clojure.core.async :refer :all])
(defn example []
(let [a (chan) ; a channel for a to report it's answer
b (chan) ; a channel for b to report it's answer
output (chan)] ; a channel for the reporter to report back to the repl
(go (<! (timeout (rand-int 1000))) ; process a
(>! a (rand-nth [true false])))
(go (<! (timeout (rand-int 1000))) ; process b
(>! b (rand-nth [true false])))
(go (>! output (and (<! a) (<! b)))) ; the reporter process
output)) ;return the channe that the result will be sent to
async-example.core> (<!! (go (<! (example))))
false
async-example.core> (<!! (go (<! (example))))
false
async-example.core> (<!! (go (<! (example))))
true
Of course this is overkill for your situation, though it's tremendously fun anyway ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With