user972946
user972946

Reputation:

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

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.

Upvotes: 7

Views: 294

Answers (1)

mtyaka
mtyaka

Reputation: 8848

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.

Upvotes: 9

Related Questions