Skip to content

Instantly share code, notes, and snippets.

@halgari
Created July 31, 2012 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save halgari/3213067 to your computer and use it in GitHub Desktop.
Save halgari/3213067 to your computer and use it in GitHub Desktop.
Async example in clojure-py
(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