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.
See Getting Started to learn how to install the Clojure CLI. You can exit the REPL by typing Ctrl+D (pressing the Ctrl and D keys at the same time).
You can just press ctrl-d (*nix) or ctrl-z (Windows) to exit the REPL.
To exit from the REPL terminal, press Ctrl + C twice or write . exit and press Enter.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With