Change binding and pmap interaction?

There are several few old blog posts that caution when mixing dynamic variables bindingand pmapfor example. here where we get the following code snippet:

user=> (def *foo* 5)
#'user/*foo*
user=> (defn adder
             [param]
             (+ *foo* param))
#'user/adder
user=> (binding [*foo* 10]
         (doseq [v (pmap adder (repeat 3 5))]
           (println v)))
10
10
10
nil

But this is not what happens when I run this code (changing the first line to (def ^:dynamic *foo* 5)). I get three 15as output (using Clojure 1.4), just as you naively expect & mdash, i.e. with a change in the binding form visible by the function passed to pmap. How has the interaction of flows and local pmap connections changed? I canโ€™t find it anywhere anywhere.

+5
source share
1 answer

1.3, pmap . , var ^: . Binding Conveyance 1.3:

from: https://github.com/clojure/clojure/blob/1.3.x/changes.txt

Clojure APIs that pass work off to other threads (e.g. send, send-off, pmap, future) now convey the dynamic bindings of the calling thread:

  (def ^:dynamic *num* 1)
  (binding [*num* 2] (future (println *num*)))
  ;; prints "2", not "1"
+5

All Articles