Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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