wrongusername
wrongusername

Reputation: 18918

Clojure program not exiting when finishing last statement?

What would cause a Clojure program to not immediately exit upon finishing the last statement in the main function?

All I did was to change a (doall (map ...)) to a (doall (pmap ...)), and suddenly the program would hang upon completion of its tasks for a good number of seconds before exiting. I would put a (println "Finished everything!") on the last line of the -main function, and it would print that, yet still not exit for some time. What could cause this behavior, and how should I fix it?

EDIT: The pmap is the only part of the program that is parallelized (mostly because everything else runs more or less instantly). As latter parts of the program require all the results from pmap in order to function correctly, and as the program output is the same for both map and pmap, I somewhat doubt that pmap would still be running at the end of the program. Putting (System/exit 0) at the end instead of the println does not change program output, either.

Upvotes: 16

Views: 2412

Answers (2)

Scott
Scott

Reputation: 17257

pmap will spin up multiple threads from a threadpool to service your concurrent tasks.

Calling shutdown-agents is necessary to allow the JVM to exit in an orderly manner because the threads in the agent threadpools are not daemon threads.

You need to explicitly tell them to gracefully shutdown (but only when your program is done). This looks to have been answered before here.

Quoting from that answer:

"You need to call shutdown-agents to kill the threads backing the threadpool used by pmap."

Docs are here: http://clojuredocs.org/clojure_core/1.3.0/clojure.core/shutdown-agents

Upvotes: 19

Jason Down
Jason Down

Reputation: 22191

According to the docs:

...[pmap is O]nly useful for computationally intensive functions where the time of f dominates the coordination overhead.

So this probably means there is more overhead using pmap for your scenario than using map. The pmap operations are probably taking a little extra time to finish up before the code after pmap fires.

Upvotes: 3

Related Questions