在Clojure中以定期的时间间隔执行代码 [英] Executing code at regularly timed intervals in Clojure
问题描述
在Clojure中,定期执行代码的最佳方法是什么?我目前使用java.util.concurrent.ScheduledExecutorService,但那是Java - 是有一个Clojure方法调度代码以定期间隔运行,延迟后,可以取消?所有Clojure代码示例我看到使用Thread / sleep,这也似乎太Java了。
What's the best way to make code run at regular intervals in Clojure ? I'm currently using java.util.concurrent.ScheduledExecutorService, but that's Java - is there a Clojure way of scheduling code to run at regular intervals, after a delay, cancellably ? All the Clojure code examples I've seen use Thread/sleep, which also seems too Java.
推荐答案
Overtone 的源代码,特别是在特定时间安排活动的代码。
这是一个音乐合成系统,所以你必须希望他们有定时代码的权利!!
It's a music synthesis system so you have to hope they have the timing code right!!
他们有帮助地将时间代码分离成一个单独的项目( overtone / at-at ),以便您可以轻松地导入它,如果你想要的。这为底层Java库提供了一个很好的Clojure包装器(即 ScheduledThreadPoolExecutor < a>和朋友)。语法如下:
Also they have helpfully separated the timing code out into a separate project (overtone/at-at) so that you can easily import it if you want. This provides a nice Clojure wrapper to the underlying Java libraries (i.e. ScheduledThreadPoolExecutor and friends). The syntax is like this:
;; run some-function every 500ms
(every 500 some-function)
在特定时间的活动:
;; run some-other-function 10 seconds from now
(at (+ 10000 (now)) some-other-function)
这篇关于在Clojure中以定期的时间间隔执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!