Why does a clojure future block the main thread? -
i have trivial lein project -main contains future:
(def f (future 42)) (defn -main [& args] (println @f)) when run lein run prints 42 not return.
i don't understand why not return ?
how lein run return ?
your question twofold:
- why lein not return?
lein hangs because thread pool backs clojure futures not use daemon threads have explicitly shut down. if change code following, should work:
(def f (future 42)) (defn -main [& args] (println @f) (shutdown-agents)) - futures block main thread
the line (println @f) can potentially block main thread when "derefing" f if future hasn't finished job yet.
this limitation of clojure futures can addressed using core.async or rxclojure. i've been working on alternative implementation of futures clojure plan open source , addresses these issues.
Comments
Post a Comment