Created
July 31, 2012 02:49
-
-
Save halgari/3213067 to your computer and use it in GitHub Desktop.
Async example in clojure-py
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-test | |
(:require urllib)) | |
(defmacro await-async [& body] | |
`(py.bytecode/YIELD_VALUE ~@body)) | |
(defn download-google | |
[] | |
(future | |
(with-open [x (urllib/urlopen "http://www.google.com")] | |
(print "Start ") | |
(.read x)))) | |
(defn async-fn [x] | |
(dotimes [t x] | |
(let [s (await-async (download-google))] | |
(println "-- Done. Downloaded " (count s) " bytes")))) | |
(let [asyncfn (async-fn 4)] | |
(loop [val (.next asyncfn)] | |
(let [async (.send asyncfn @val)] | |
(print "|") ; Here we're in the control loop | |
(recur async)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment