【问题标题】:What's the simplest way to run a task periodically in clojure在clojure中定期运行任务的最简单方法是什么
【发布时间】:2016-01-24 23:22:32
【问题描述】:

我想每 10 秒执行一次函数。怎么做最简单的方法?在 core.async 中,有一个超时功能,它最多会等待那个时间,而我想要的是至少等待那个时间。

【问题讨论】:

标签: clojure core.async


【解决方案1】:

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)))))

【讨论】:

    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多