Skip to content

Instantly share code, notes, and snippets.

@rsiddle
Last active August 29, 2015 14:11
Show Gist options
  • Save rsiddle/f9e746a5a4e3abd624ab to your computer and use it in GitHub Desktop.
Save rsiddle/f9e746a5a4e3abd624ab to your computer and use it in GitHub Desktop.
(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