Now I have this code which I'm getting stuck at:
(Please note I know that the list of sites I'd normally expect to be something from a DB/text file. I'm just trying to get it working without adding extra things to think about)
So get-head-response-for-sites is where I'm trying to do #2. It gets me a list of futures that I can use. Where I'm having trouble is that the current println line, which is where I'm trying to deal with #3, blocks due to deref-ing so it's basically not really all that different than if I did it non threading.
What I (think) I need is something that keeps looping through all the futures, checking their status, and println’ing the result when something has come back. This will be repeated until all the futures are done. The reason I want this is that for example if the first site takes 3 minutes to respond I want the other two sites to print their HEAD content as soon as it’s retrieved. Here's what I'm trying to figure out in order of importance:
Thanks for any and all response. Once again I apologize for the not so pro code but some code is better than nothing I hope.
- Chris White ( @cwgem )
(ns cjr-http-test.core (:require [clj-http.client :as client]
[clojure.core.async :as async :refer [<!! >!!]])) (defn get-heads [sites] (mapv (fn [site] (future {:head (client/head site) :url site})) sites)) (def sites ["http://www.google.com" "http://www.yahoo.com" "http://www.bing.com"]) (defn use-futures [] (let [head-requests (get-heads sites)] (loop [to-check head-requests checked []] (cond (and (empty? checked) (empty? to-check)) :finished (empty? to-check) (recur checked []) (realized? (first to-check)) (do (-> to-check first deref :url println) (recur (rest to-check) checked)) :else (recur (rest to-check) (cons (first to-check) checked)))))) (defn use-async [] (let [ch (async/chan) producers (mapv (fn [site] (doto (Thread. #(>!! ch {:head (client/head site) :url site})) (.start))) sites) close-chan (doto (Thread. (fn [] (mapv #(.join %) producers) (async/close! ch))) (.start))] (loop [v (<!! ch)] (if (nil? v) :finished (do (-> v :url println) (recur (<!! ch)))))))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.