Skip to content

Instantly share code, notes, and snippets.

@hyone
Created February 15, 2012 12:24
Show Gist options
  • Save hyone/1835356 to your computer and use it in GitHub Desktop.
Save hyone/1835356 to your computer and use it in GitHub Desktop.
Multiple async HTTP requests by aleph
;; (defproject async-test2 "1.0.0-SNAPSHOT"
;; :description "FIXME: write description"
;; :dependencies [[org.clojure/clojure "1.3.0"]
;; [aleph "0.2.1-alpha1"]])
(ns async-test2.core
(:use lamina.core
aleph.http))
(defn gen-url [keyword]
(format "http://www.google.com/search?q=%s" keyword))
(def urls (concat (map gen-url ["Clojure" "Haskell" "OCaml" "Scala" "F#"])
["http://localhost:3000/"]
(map gen-url ["Ruby" "Python" "Perl" "PHP" "JavaScript"])))
(defn async-fetch [urls]
(let [start (System/nanoTime)
log (fn [i url status]
(println (format "%8.3f: %2d: %s => %d"
(/ (- (System/nanoTime) start) 1000000.0)
i url status)))]
(for [[url i] (map vector urls (map inc (range)))]
(async
(let [res (http-request {:method :get :url url})]
(log i url (:status res))
res)))))
(defn -main [& args]
(let [resps (doall (async-fetch urls))]
;; waiting all async http fetch
(doall (map deref resps))
(println "Finish.")
(System/exit 0)))
;; To check async process, prepare long time request
;; $ plackup -p 3000 -e 'sub { sleep 5; return [200, ["Content-Type" => "text/plain"], ["Hello World."]] }'
;; $ lein run -m async-test2.core
;; 755.716: 2: http://www.google.com/search?q=Haskell => 200
;; 757.581: 4: http://www.google.com/search?q=Scala => 200
;; 755.678: 3: http://www.google.com/search?q=OCaml => 200
;; 762.821: 1: http://www.google.com/search?q=Clojure => 200
;; 880.444: 5: http://www.google.com/search?q=F# => 200
;; 885.692: 7: http://www.google.com/search?q=Ruby => 200
;; 917.976: 9: http://www.google.com/search?q=Perl => 200
;; 935.877: 10: http://www.google.com/search?q=PHP => 200
;; 941.650: 8: http://www.google.com/search?q=Python => 200
;; 945.360: 11: http://www.google.com/search?q=JavaScript => 200
;; 5729.774: 6: http://localhost:3000/ => 200
;; Finish.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment