aroemers / mount-lite

mount, but different and light
https://cljdoc.org/d/functionalbytes/mount-lite/
Eclipse Public License 1.0
102 stars 8 forks source link

How to handle multiple system of states with threadpool? #13

Closed didibus closed 7 years ago

didibus commented 7 years ago

The documentation says: There are simple ways to work around this though, and mount-lite may even provide utilities for this at a later stage.

But I can't figure out how to work around it?

aroemers commented 7 years ago

Hi @didibus,

It depends a bit on your situation, but binding the state(s) you need to the code you are going to execute in a thread(pool) is in many cases the solution to this. If you need more advise, I think a bit of source code might help.

Cheers

didibus commented 7 years ago

I'm just trying to understand, to make sure if I depend on mount-lite, I won't run into issues I can't address.

Here was my test:

(ns mount-test.core
  (:require [mount.lite :as mount :refer [defstate]]
            [clojure.edn :as edn]
            [clojure.tools.namespace.repl :as tn]
            [clojure.repl :refer [pst]]))

(defn go []
  (mount/start)
  :ready)

(defn reset []
  (mount/stop)
  (tn/refresh :after `go))

(defn load-config [url]
  (edn/read-string (slurp url)))

(defstate config :start (load-config "resources/config.edn"))

(defn foo []
  (case (:do @config)
        :a1 :a1
        :a2 :a2))

(mount/with-session
 (mount/with-substitutes [#'config (mount/state :start {:do :a2})]
                         (mount/start))
 (dorun (pmap (fn [_] (println (foo))) [1])))
aroemers commented 7 years ago

Hi @didibus,

Sorry for not responding sooner; busy times. By binding the states you are going to use in an existing thread pool, your test would run as expected:

(defn foo [config]
  (case (:do config)
    :a1 :a1
    :a2 :a2))

(mount/with-session
  (mount/with-substitutes [#'config (mount/state :start {:do :a2})]
    (mount/start))
  (let [config @config]
    (doall (pmap (fn [_] (println (foo config))) [1]))))

Does that make sense to you?

aroemers commented 7 years ago

Closing for now @didibus, please re-open if this is still an issue