The "won't terminate" part is due to background threads being created by the 
call to pmap, and by default it will wait 60 seconds before exiting.  If you 
don't wait that long, it definitely appears like it is hung.  If you look on 
the ClojureDocs entry for pmap, the last example says to see the examples for 
"future", which describe why this happens, and how you can avoid the 60-second 
wait by calling (shutdown-agents) at the end.

    http://clojuredocs.org/clojure_core/clojure.core/future

At the REPL, realize that REPL stands for Read, Eval, Print, and Loop.  Every 
expression you enter in the REPL is read, evaluated, *the result is printed*, 
and then loop (start over).

When you put multiple expressions in a file, each expression is read and 
evaluated, but its result is not printed unless you ask for it.  Because map 
and pmap are lazy, and nothing is done with its return value, it only evaluates 
part of the answer and then stops.  If you print the result of the pmap, or 
wrap it in (dorun (pmap ...)) or (doall (pmap ...)), then you should get the 
same results when running it from a file as you get in the REPL.

Andy

On Nov 25, 2012, at 4:44 PM, Felipe Coelho wrote:

> I'm trying to learn a bit about Clojure and it's concurrency features but 
> I've hit an issue at an early point, where the same program exhibits 
> different behavior if run within the REPL or from a regular file. This is the 
> code I'm running:
> 
> (def foo (atom 0))
> (pmap
>     (fn [_] (swap! foo inc))
>     (range 10000))
> (println @foo) 
> 
> When I run this inside the REPL, the (pmap ...) returns the whole sequence 
> with 10000 elements and the last line prints "10000", as expected. But when I 
> put this code in a file and run it as one of
>  
> java -cp clojure-1.4.0.jar clojure.main swap.clj
> java -jar clojure-1.4.0-slim.jar swap.clj 
> 
> then it will output "32" and the program won't terminate, I have to hit 
> CTRL+C to get control back to the console. Am I doing something wrong here? 
> I'm running Clojure 1.4.0 with Java 1.6.0_21 in Windows.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to