Clojure core.async, any way to control number of threads in that (go…) thread pool?

流过昼夜 提交于 2019-11-30 05:23:41

In the current Clojure version of core.async, the thread pool executor is located in the clojure.core.async.impl.dispatch namespace. You can alter the executor var and supply a custom thread pool ExecutorService.

(ns sandbox
  (:require [clojure.core.async.impl.concurrent :as conc]
            [clojure.core.async.impl.exec.threadpool :as tp]
            [clojure.core.async :as async]))

(defonce my-executor
  (java.util.concurrent.Executors/newFixedThreadPool
   1
   (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))))

(async/go
 (println 
  (Thread/currentThread))) ;=> #<Thread Thread[my-async-dispatch-1,5,main]>

Note: Core.async is still in alpha, so, hopefully, this will change in the future.

The current accepted answer was valid up to this commit so basically now you have two cases:

  • If you want to just change the max number of threads in the pool, you pass the number as Java property clojure.core.async.pool-size (it defaults to 8)

  • If you want to replace the ExecutorService, you use the same trick of alter-var-root but targeting the new implementation (there a protocol to implement):

    (ns your-app.threadpool
      (:require [clojure.core.async.impl.protocols :as protocols]
                [clojure.core.async.impl.concurrent :as conc]
                [clojure.core.async.impl.exec.threadpool :as tp])
      (:import java.util.concurrent.Executors))
    
    (defonce my-executor
      (let [executor-svc (Executors/newFixedThreadPool
                          1
                          (conc/counted-thread-factory "async-dispatch-%d" true))]
        (reify protocols/Executor
           (protocols/exec [this r]
             (.execute executor-svc ^Runnable r)))))
    
    (alter-var-root #'clojure.core.async.impl.dispatch/executor
                    (constantly (delay my-executor)))
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!