Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active September 27, 2017 12:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/e73b0959c5468fdf081df3f6bcd6dc3f to your computer and use it in GitHub Desktop.
Save 0atman/e73b0959c5468fdf081df3f6bcd6dc3f to your computer and use it in GitHub Desktop.
Hy implamentation of Clojure's awesome `future` call
(import [multiprocessing.pool [ThreadPool]])
(defmacro future [code]
(def t (gensym))
(def pool (ThreadPool :processes 1))
`(do
(def ~t (pool.apply-async (fn [] ~code)))
~t))
@0atman
Copy link
Author

0atman commented Aug 17, 2017

usage:

=> (def my-thread (future (long-running-function)))
# some time passes
=> (.get my-thread)
"return value from long-running-function"

@0atman
Copy link
Author

0atman commented Aug 17, 2017

or simply,

=> (future (print "hy"))
<multiprocessing.pool.ApplyResult object at 0x7f946bff56a0>
"hy"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment