Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on answer for another related question <a href="https://stackoverflow.com/questions/18779296/clojure-core-async-any-way-to-control-number-of-threads-in-that-go-thread">How to control number of threads in (go...)</a>, I've found a better way to properly kill all the threads started by (go...) block:</p> <p>First alter the executor var and supply a custom thread pool </p> <pre><code>;; 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)))) </code></pre> <p>Then call .shutdownNow and .awaitTermination method at the end of (go...) block</p> <pre><code>(.shutdownNow my-executor) (while (not (.awaitTermination my-executor 10 java.util.concurrent.TimeUnit/SECONDS ) ) (prn "...waiting 10 secs for executor pool to finish") ) </code></pre> <p><strong>[UPDATE]</strong> 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 <a href="https://github.com/flatland/clojail/blob/master/src/clojail/core.clj#L26" rel="nofollow noreferrer">thunk-timeout</a> function. Credits go to <a href="https://stackoverflow.com/questions/6694530/executing-a-function-with-a-timeout/6697356#6697356">this post</a>. Example below</p> <pre><code>(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 (&gt;! c (toSendToGo args timeoutUnits)))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload