3

I have an application which has an initial flow that can be done in parallel:

  1. Fetch two JSON documents (I use clj-http for that)
  2. Parse these documents (extract only required data)
  3. Join the results
  4. Dump them into a one file

So there's something like that:

some-entry-point /\ / \ / \ / \ fetchA fetchB | | | | parseA parseB \ / \ / \ / \ / \/ join | | dump 

What is the correct and the most current way to achieve that?

What I found so far are:

1 Answer 1

4

Since you have exactly two branches here, it'll be best to dispatch parallel jobs to separate threads using future function. future will return you a future object (a special promise which will be automatically resolved when the job will be completed).

Here is how it will look:

(defn some-entry-point [obja objb] (let [pa (-> (fetchA obja) parseA future) pb (-> (fetchB objb) parseB future)] (-> (join @pa @pb) dump))) 

@ here is a shortcut (reader macro) for deref function.

Or course, you could execute one of you branches in main thread, creating only one future object:

(defn some-entry-point [obja objb] (let [p (-> (fetchB objb) parseB future)] (-> (fetchA obja) parseA (join @p) dump))) 

You could also generalize this approach to fetch and parse multiple objects using some general fetch and parse functions, using pmap for paralelization:

(defn some-entry-point "Fetch and parse multiple objects in parallel" [objs] (->> objs (pmap (comp parse fetch)) (apply join) dump)) 
Sign up to request clarification or add additional context in comments.

6 Comments

Alright, it seems to be working, but why do I need to (shutdown-agents) to let my program exit immediately? I have an issue described here: stackoverflow.com/questions/27014241/…
@squixy just checked it on my Ubuntu 14.04.2 LTS with Clojure 1.6.0 and Leiningen 2.5.1 on Java 1.8.0_45 Java HotSpot(TM) 64-Bit Server VM and everything worked fine without (shutdown-agents). It looks like some platform/JVM specific problem. Try updating your JVM. If it won't help, then you'll have to call (shutdown-agents) explicitly, though it doesn't look like a big problem to me.
lein -v => Leiningen 2.5.1 on Java 1.8.0_40 Java HotSpot(TM) 64-Bit Server VM so idk why is that.
A correction: future does not return a promise, it returns a future. See also the future? predicate.
I thought they had changed this, but Clojure agents use non-daemon threads (here: clojure.org/agents search for 'daemon'). The JVM will not shut down if there are non-daemon threads running.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.