Created
November 4, 2016 02:37
-
-
Save jscruggs/ce110d17c8db41c45606bd1e663a8808 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 async-example.core | |
(:gen-class) | |
(:require [clojure.core.async | |
:as a | |
:refer [>! <! >!! <!! go chan buffer close! thread | |
alts! alts!! timeout]])) | |
(def write-to-file-channel (chan 10000)) | |
(defn get-contents [name] | |
(Thread/sleep (+ 40 (rand 30))) ;waiting for a service | |
(str (java.util.UUID/randomUUID))) | |
(defn write-to-file [name] | |
(clojure.core/spit (clojure.java.io/file "/tmp/async-example/" name) ; /tmp/async-example/ must exist | |
(get-contents name))) | |
(defn -main [number-of-workers] | |
(dotimes [n 10000] | |
(go (>! write-to-file-channel (str n ".txt")))) | |
(dotimes [n (read-string number-of-workers)] | |
(thread | |
(while true | |
(write-to-file (<!! write-to-file-channel))))) | |
(println "Done in main. Taking a nap.") | |
(Thread/sleep 50000) ; The program continues doing other things | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment