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

后端 未结 2 1620
故里飘歌
故里飘歌 2020-12-29 15:26

By default (go..) will use twice the number of cores + 42 threads for the thread pool. Is there any way I can set the number of threads, or number of CPUs that the code can

2条回答
  •  情深已故
    2020-12-29 16:02

    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))) ;=> #
    

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

提交回复
热议问题