Created
May 17, 2015 22:07
-
-
Save raspasov/7c9d8f2872d6065b2145 to your computer and use it in GitHub Desktop.
example of blocking core.async pipeline
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
;add (:require [clojure.core.async :refer [chan close! go >! <! <!! >!! go-loop put! thread alts! alts!! timeout pipeline pipeline-blocking pipeline-async]]) | |
;chans | |
(def to-ch (chan)) | |
(def from-ch (chan)) | |
;data looks like {url some-data, etc} | |
(def my-map (atom {})) | |
(defn fetch-from-url [{:keys [url]}] | |
(println "going to fetch from " url) | |
(let [data (slurp url)] | |
;return | |
{:url url :data data})) | |
;save to atom one at a time | |
(go (loop [] | |
(let [{:keys [url data]} (<! to-ch)] | |
(swap! my-map assoc url data) | |
(recur)))) | |
;pipeline with 10 parallelism | |
(def pipeline-1 (pipeline-blocking 10 to-ch (map fetch-from-url) from-ch)) | |
(def urls ["http://google.com" | |
"http://yahoo.com" | |
"http://facebook.com" | |
"http://amazon.com"]) | |
;start the processing | |
(doseq [url urls] | |
(>!! from-ch {:url url})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great stuff!