问题描述
默认情况下 (go..) 将使用 两倍于线程池的内核数 + 42 个线程.有什么办法可以通过设置环境变量或某事来设置代码可以使用的线程数或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 机器上,我可以使用 taskset 设置 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)
.
推荐答案
在当前 Clojure 版本的 core.async 中,线程池执行器位于 clojure.core.async.impl.dispatch
命名空间.您可以更改 executor
变量并提供自定义线程池 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,有什么方法可以控制该(去...)线程池中的线程数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!