Take the 2-minute tour ×

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.

share|improve this question
add comment

3 Answers

up vote 18 down vote accepted

Well worth looking at the source code for Overtone, in particular the code for scheduling events at a particular time.

It's a music synthesis system so you have to hope they have the timing code right!!

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)

You can also shedule events at specific times:

;; run some-other-function 10 seconds from now
(at (+ 10000 (now)) some-other-function)
share|improve this answer
ha! it's almost the same as the code i've written! –  Hendekagon Nov 22 '11 at 2:53
Great minds think alike perhaps :-) If you have good additions then do contribute back to overtone/at-at, would be great if we could develop a single great timing library in Clojure! –  mikera Nov 22 '11 at 2:57
hmm not quite...same function names but a different approach. mine depends on clj-time...at-at looks good - thanks! –  Hendekagon Nov 22 '11 at 3:02
although, it's not quite what i need, as I also need things to be done at specific times on certain days –  Hendekagon Nov 22 '11 at 4:02
Great answer, I was thinking about requiring something like this in one of my projects, will be sure to check out overtone/at-at! –  djhworld Nov 25 '11 at 8:34
show 1 more comment

From the clojure website http://clojure.org/concurrent_programming:


It sounds like you are already doing it the right way.

(import 'java.util.concurrent.Executors)
(import 'java.util.concurrent.TimeUnit) 
(.scheduleAtFixedRate (Executors/newScheduledThreadPool 1) 
  #(println "Hello") 0 5 TimeUnit/SECONDS)
share|improve this answer
ok, but it feels silly to be dropping into Java to do something so ordinary! –  Hendekagon Nov 22 '11 at 1:20
One of the Clojure principals is to embrace the host platform (where it is powerful enough). In this case the clojure answer would be unlikely to go less than one expression. –  Arthur Ulfeldt Nov 22 '11 at 2:04
interop is great, and certainly using Java makes sense for this, but I'd at least expect a wrapper for aesthetic reasons –  Hendekagon Nov 22 '11 at 2:55
IMHO here Java is not powerful enough: ScheduledExecutorService has 3 (4) methods and only one of them is overloaded to accept Callable instead of Runnable, with the unexpected outcome that exceptions in your scheduled function will be silently swallowed (without even printing anything to stderr!)... Obviously I don't really care about the result of a repeated scheduled function, so I don't really need a whole Callable... I had a look at at-at and unfortunately it seems that it doesn't wrap its fn argument in a try-catch-printstacktrace either –  berdario May 9 at 9:17
add comment

I answered my own question @ Implementing a cron type scheduler in clojure

maybe cronj might help?

share|improve this answer
add comment

Your Answer


