(whe-6ren">
gpt4 book ai didi

clojure - 有没有办法在 clojure future 结束时收到通知?

转载 作者:行者123 更新时间:2023-12-03 00:27:33 24 4
gpt4 key购买 nike

有没有办法对 future 设置监视,以便在完成时触发回调?

类似这样的东西吗?

> (def a (future (Thread/sleep 1000) "Hello World!")
> (when-done a (println @a))

...waits for 1sec...
;; => "Hello World"

最佳答案

您可以启动另一个任务来观察 future ,然后运行该函数。在这种情况下,我将使用另一个 future。这很好地包裹在一个完成后的函数中:

user=> (defn when-done [future-to-watch function-to-call] 
(future (function-to-call @future-to-watch)))
user=> (def meaning-of-the-universe
(let [f (future (Thread/sleep 10000) 42)]
(when-done f #(println "future available and the answer is:" %))
f))
#'user/meaning-of-the-universe

... waiting ...

user=> future available and the answer is: 42
user=> @meaning-of-the-universe
42

关于clojure - 有没有办法在 clojure future 结束时收到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16349415/

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