问题描述
默认情况下(go ..)将使用线程。有没有什么方法可以通过设置环境变量或sth设置线程的数量或代码可以使用的CPU数量?
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 use, through setting an environment variable or sth?
在linux机器上,我可以使用设置CPU的数量,例如
taskset -c 0,1 my_Java_or_Clojure_program
,虽然taskset似乎对( - >(java.lang。 Runtime / getRuntime).availableProcessors)
。
On linux machine I can set number of CPU using taskset, e.g.taskset -c 0,1 my_Java_or_Clojure_program
, although taskset seems not effective on the number returned by (-> (java.lang.Runtime/getRuntime) .availableProcessors)
.
推荐答案
在core.async的当前Clojure版本中,线程池执行器位于 clojure .core.async.impl.dispatch
命名空间。您可以更改 executor
var并提供自定义线程池 ExecutorService
。
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]>
注意: Core.async仍然是alpha的,这将在未来改变。
Note: Core.async is still in alpha, so, hopefully, this will change in the future.
这篇关于Clojure core.async,任何方式来控制线程数量(go ...)线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!