Skip to content

Instantly share code, notes, and snippets.

/async.clj Secret

Created July 24, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a7c47cd7b38c2e71a9bd to your computer and use it in GitHub Desktop.
Save anonymous/a7c47cd7b38c2e71a9bd to your computer and use it in GitHub Desktop.
(require
'[clojure.core.async
:as
a
:refer
[>! <! >!! <!! go chan buffer close! thread alts! alts!! timeout]])
(defn make-printer []
(println "starting printer")
(let [in-chan (chan) off-chan (chan)]
(go-loop
[]
(let [[v ch] (alts! [off-chan in-chan])]
(if (= ch off-chan)
(do
(println (str "Stopping... msg: " v))
(close! in-chan)
(close! off-chan)
(println "Stopped"))
(do (println (str "Input: " v)) (recur)))))
{:in in-chan,
:off off-chan,
:stop (fn stop! [] (>!! off-chan "turn off please")),
:send (fn send! [msg] (>!! in-chan msg))}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment