Skip to content

Instantly share code, notes, and snippets.

@oubiwann
Created October 28, 2012 02:48
Show Gist options
  • Select an option

  • Save oubiwann/3967254 to your computer and use it in GitHub Desktop.

Select an option

Save oubiwann/3967254 to your computer and use it in GitHub Desktop.
Exploring Agents in Clojure, Part I
(def read-agent (agent nil))
(send-off read-agent big-read 10)
(defn big-read [old-value seconds]
"Read a really big file"
(time (Thread/sleep (* seconds 1000))))
"big file data")
(def read-agent (agent nil))
(defn big-read [old-value seconds]
"Read a really big file"
(time (Thread/sleep (* seconds 1000)))
"big file data")
$ start-clojure
Clojure 1.4.0
user=> (ns async-1)
nil
async-1=> (load-file "04-agent.clj")
#'async-1/big-read
async-1=> (send-off read-agent big-read 10)
#<Agent@54128635: nil>
async-1=> @read-agent
nil
async-1=> "Elapsed time: 10001.577 msecs"
async-1=> read-agent
#<Agent@54128635: "big file data">
async-1=> @read-agent
"big file data"
async-1=> (shutdown-agents)
nil
async-1=> ^D
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment