Why does this simple main method never return when run by leiningen?
Asked Answered
S

1

7

This piece of code returns immediately:

user=> (dorun (pmap + [1 2] [3 4]))
nil

However, when I run the same piece of code in main method using lein:

(ns practice.core)

(defn -main [& args]
  (dorun (pmap + [1 2] [3 4])))

why does it never return?

Interestingly, if I replace pmap by map, it returns normally.

Sheliasheline answered 13/5, 2013 at 9:47 Comment(0)
L
9

You need to call shutdown-agents at the end of your -main method.

(defn -main [& args]
  (dorun (pmap + [1 2] [3 4]))
  (shutdown-agents))

This is mentioned on http://clojure.org/agents:

Note that use of Agents starts a pool of non-daemon background threads that will prevent shutdown of the JVM. Use shutdown-agents to terminate these threads and allow shutdown.

pmap uses futures which run on the agent thread pool.

Limen answered 13/5, 2013 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.