gpt4 book ai didi

clojure - 为什么这个简单的 main 方法在 leiningen 运行时永远不会返回?

转载 作者:行者123 更新时间:2023-12-02 17:01:06 24 4
gpt4 key购买 nike

这段代码立即返回:

user=> (dorun (pmap + [1 2] [3 4]))
nil

但是,当我使用 lein 在 main 方法中运行相同的代码时:

(ns practice.core)

(defn -main [& args]
(dorun (pmap + [1 2] [3 4])))

为什么它永远不会返回?

有趣的是,如果我用 map 替换 pmap,它会正常返回。

最佳答案

您需要在 -main 方法末尾调用 shutdown-agents

(defn -main [& args]
(dorun (pmap + [1 2] [3 4]))
(shutdown-agents))

http://clojure.org/agents 中提到了这一点:

Note that use of Agents starts a pool of non-daemon background threads that will prevent shutdown of the JVM. Use shutdown-agents to terminate these threads and allow shutdown.

pmap 使用在代理线程池上运行的 future。

关于clojure - 为什么这个简单的 main 方法在 leiningen 运行时永远不会返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com