Clojure core.async, CPU hangs after timeout. Anyway to properly kill macro thread produced by (go..) block? Clojure core.async, CPU hangs after timeout. Anyway to properly kill macro thread produced by (go..) block? multithreading multithreading

Clojure core.async, CPU hangs after timeout. Anyway to properly kill macro thread produced by (go..) block?


In REPL there seems to be no clean way yet.

I first tried a very dirty way by using deprecated method Thread.stop

 (doseq [i @threadpool ]              (.stop i))

It seemed worked as CPU usage dropped once the main thread returned to REPL, but if I run the program again in REPL, it'd just hang at the go block part!!

Then I googled around and found this blog and it says

One final thing to note: we don't explicitly do any work to shutdown the go routines. Go routines will automatically stop operation when the main function exits. Thus, go routines are like daemon threads in the JVM (well, except for the "thread" part ...)

So I tried again by making my project into a uberjar and run it on a command console, and it turned out that CPU usage would drop immediately when blinking cursor returns to the console!


Based on answer for another related question How to control number of threads in (go...), I've found a better way to properly kill all the threads started by (go...) block:

First alter the executor var and supply a custom thread pool

;; def, not defonce, so that the executor can be re-defined;; Number of threads are fixed to be 4(def my-executor  (java.util.concurrent.Executors/newFixedThreadPool   4   (conc/counted-thread-factory "my-async-dispatch-%d" true)))(alter-var-root #'clojure.core.async.impl.dispatch/executor                (constantly (delay (tp/thread-pool-executor my-executor))))

Then call .shutdownNow and .awaitTermination method at the end of (go...) block

(.shutdownNow my-executor)(while (not  (.awaitTermination  my-executor 10 java.util.concurrent.TimeUnit/SECONDS ) )       (prn "...waiting 10 secs for executor pool to finish") )

[UPDATE]The shutdown executor method above seems not pure enough. The final solution for my case is to send a function with control of its own timeout into go block, using thunk-timeout function. Credits go to this post. Example below

(defn toSendToGo [args timeoutUnits]  (let [result (atom nil)          timeout? (atom false)]    (try      ( thunk-timeout        (fn []  (reset! result  (myFunction args))) timeoutUnits)      (catch  java.util.concurrent.TimeoutException e  (do  (prn "!Time out after " timeoutUnits " seconds!!") (reset! timeout? true))     ))    (if @timeout?  (do sth))    @result))(let [c ( chan)]  (go (>! c (toSendToGo args timeoutUnits))))


(shutdown-agents)

Implementation-specific, JVM: both agents and channels use a global thread pool, and the termination function for agents iterates and closes all open threads in the VM. Empty the channels first: this action is immediate and non-reversible (especially if you are in a REPL).