Last active
August 29, 2015 14:17
-
-
Save Bill/33df1cc70abb30964397 to your computer and use it in GitHub Desktop.
Output throttling for https://github.com/Intervox/clj-progress
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 progress-light.core | |
(:require [clj-progress.core :as progress] | |
[clojure.core.async :as async :refer [>!! timeout chan alt!! thread]]) | |
(:gen-class)) | |
(def tick-ch | |
(chan)) | |
(def done-ch | |
(chan)) | |
(defn monitor-progress | |
([max-ticks] | |
"monitor up to max-ticks; inform every 1000 milliseconds (i.e. 1 second)" | |
(monitor-progress max-ticks 1000)) | |
([max-ticks inform-every] | |
"monitior up to max-ticks; inform every progress-every milliseconds" | |
(progress/init "Processing" max-ticks) | |
(thread (loop [progress 0 | |
timer (timeout inform-every)] | |
(alt!! | |
tick-ch (recur (inc progress) timer) | |
done-ch (progress/done) ;; and exit thread | |
timer (do (when (pos? progress) ;; can't tick-to 0 | |
(progress/tick-to progress)) | |
(recur progress (timeout inform-every)))))))) | |
(defn tick [& [obj]] (>!! tick-ch true) obj) | |
(defn done [& [obj]] (>!! done-ch true) obj) | |
(defn -main [& args] | |
(monitor-progress 1000) | |
(dotimes [n 1000] (Thread/sleep 10) (tick)) | |
(done) | |
(System/exit 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment