Created
October 28, 2012 02:48
-
-
Save oubiwann/3967254 to your computer and use it in GitHub Desktop.
Exploring Agents in Clojure, Part I
This file contains hidden or 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
| (def read-agent (agent nil)) |
This file contains hidden or 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
| (send-off read-agent big-read 10) |
This file contains hidden or 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
| (defn big-read [old-value seconds] | |
| "Read a really big file" | |
| (time (Thread/sleep (* seconds 1000)))) | |
| "big file data") |
This file contains hidden or 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
| (def read-agent (agent nil)) | |
| (defn big-read [old-value seconds] | |
| "Read a really big file" | |
| (time (Thread/sleep (* seconds 1000))) | |
| "big file data") |
This file contains hidden or 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
| $ 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