Last active
August 29, 2015 14:11
-
-
Save rsiddle/f9e746a5a4e3abd624ab to your computer and use it in GitHub Desktop.
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
(ns webscraper.core | |
(:gen-class) | |
(:require [clojure.core.async :as async])) | |
(defn process [line] | |
line) | |
(def stdin-reader | |
(java.io.BufferedReader. *in*)) | |
(def in-chan (async/chan)) | |
(def out-chan (async/chan)) | |
(defmacro async/threads | |
[num-consumers & body] | |
`(dotimes [_ num-consumers] | |
(async/thread ~@body))) | |
(defn start-async-consumer | |
[num-consumers] | |
(async/thread num-consumers | |
(while true | |
(let [line (async/<!! in-chan) | |
data (process line)] | |
(async/>!! out-chan data))))) | |
(defn start-async-aggregator | |
"Take items from the out-chan and print it." | |
[] | |
(async/thread | |
(while true | |
(let [data (async/<!! out-chan)] | |
(println data))))) | |
(defn -main | |
"Starts the workers" | |
[& args] | |
(do | |
(start-async-consumers 2) | |
(start-async-aggregator) | |
(doseq [line (line-seq stdin-reader)] | |
(async/>!! in-chan line)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment