在 clojure 中定期运行任务的最简单方法是什么



我想每10秒执行一次函数。如何以最简单的方法做到这一点?在core.async中,有一个超时函数最多会等待那个时间,我想要的是至少等待那个时间。

at-at 是我的最爱。以下示例是从网页复制的:

(use 'overtone.at-at)
(def my-pool (mk-pool))
(at (+ 1000 (now)) #(println "hello from the past!") my-pool)

如果你一般使用 core.async,chime 也很棒。再次,取自他们的例子:

(:require [chime :refer [chime-ch]]
          [clj-time.core :as t]
          [clojure.core.async :as a :refer [<! go-loop]])
    (let [chimes (chime-ch [(-> 2 t/secs t/from-now)
                        (-> 3 t/secs t/from-now)])]
    (a/<!! (go-loop []
           (when-let [msg (<! chimes)]
             (prn "Chiming at:" msg)
             (recur)))))

最新更新