Skip to content

Instantly share code, notes, and snippets.

@katox
Created March 14, 2014 12:38
Show Gist options
  • Save katox/9546903 to your computer and use it in GitHub Desktop.
Save katox/9546903 to your computer and use it in GitHub Desktop.
(defmacro bg
"Runs the body in a background thread.
Creates two vars, one named bg<N> and the other bg<N>'.
The first will eventually contain the result of the computation,
or any exception that is thrown.
The second is a map that prints with status & runtime information.
Returns the name of the first var."
[& body]
(let [base (str "bg" (swap! bg-id-counter inc))
sym (symbol base)
sym' (symbol (str base \'))]
`(do (println "Starting background task" '~sym)
(def ~sym)
(def ~sym')
(run-and-report (var ~sym) (var ~sym') '~sym (fn [] ~@body))
'~sym)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment