Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure program not exiting when finishing last statement?

Tags:

clojure

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.

like image 591
wrongusername Avatar asked Jan 01 '12 21:01

wrongusername


People also ask

How do I exit Clojure?

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).

How do you abruptly stop execution in the REPL?

You can just press ctrl-d (*nix) or ctrl-z (Windows) to exit the REPL.

What is the command for quitting the REPL terminal?

To exit from the REPL terminal, press Ctrl + C twice or write . exit and press Enter.


1 Answers

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

like image 131
Scott Avatar answered Sep 20 '22 05:09

Scott