18

What would be an idiomatic way of executing a function within a time limit? Something like,

(with-timeout 5000
 (do-somthing))

Unless do-something returns within 5000 throw an exception or return nil.

EDIT: before someone points it out there is,

clojure (with-timeout ... macro)

but with that the future keeps executing that does not work in my case.

Community
  • 1
  • 1
Hamza Yerlikaya
  • 49,047
  • 44
  • 147
  • 241

6 Answers6

17

What about?

    (defn timeout [timeout-ms callback]
     (let [fut (future (callback))
           ret (deref fut timeout-ms ::timed-out)]
       (when (= ret ::timed-out)
         (future-cancel fut))
       ret))

    (timeout 100 #(Thread/sleep 1000))

    ;=> :user/timed-out
Jeroen van Dijk
  • 1,029
  • 10
  • 16
17

I think you can do this reasonably reliably by using the timeout capability within futures:

  (defmacro with-timeout [millis & body]
    `(let [future# (future ~@body)]
      (try
        (.get future# ~millis java.util.concurrent.TimeUnit/MILLISECONDS)
        (catch java.util.concurrent.TimeoutException x# 
          (do
            (future-cancel future#)
            nil)))))

A bit of experimenting verified that you need to do a future-cancel to stop the future thread from continuing to execute....

mikera
  • 105,238
  • 25
  • 256
  • 415
14

This isn't something you can do 100% reliably on the JVM. The only way to stop something after a while is to give it a new thread, and then send that thread an exception when you want it to stop. But their code can catch the exception, or they can spin up another thread that you don't control, or...

But most of the time, and especially if you control the code that's being timed out, you can do something like we do in clojail:

If you wanted to make that prettier you could define a macro like

(defmacro with-timeout [time & body]
  `(thunk-timeout (fn [] ~@body) ~time))
amalloy
  • 89,153
  • 8
  • 140
  • 205
6

It's a quite a breeze using clojure's channel facilities https://github.com/clojure/core.async

require respective namespace

(:require [clojure.core.async :refer [>! alts!! timeout chan go]])

the function wait takes a timeout [ms], a function [f] and optional parameters [args]

(defn wait [ms f & args]
  (let [c (chan)]
    (go (>! c (apply f args)))
    (first (alts!! [c (timeout ms)]))))

third line pops off the call to f to another thread. fourth line consumes the result of the function call or (if faster) the timeout.

consider the following example calls

(wait 1000 (fn [] (do (Thread/sleep 100) 2)))
=> 2

but

(wait 50 (fn [] (do (Thread/sleep 100) 2)))
=> nil
  • i dont understand why this (old) answer isn't upvoted more. I came to this thread looking for some builtin that is easier than the core.async way (which i normally use). But all the answers with more votes seem like more hassle to me. – mat_dw Mar 07 '22 at 12:40
  • @mat_dw one reason this might not be the most upvoted is that the core.async version here does not actually cancel the execution of `f`. If `f` is a long running function which drains resources, it will continue to execute even though the calling thread has returned – bhurlow Jun 14 '22 at 16:54
0

You can probably use an agent, and then await-for it.

Miki Tebeka
  • 13,428
  • 4
  • 37
  • 49
-2

Adding a possible (macro-less) alternative to the mix (though the macro isn't required in the accepted answer of course)

(defn with-timeout [f ms]
  (let [p (promise)
        h (future
            (deliver p (f)))
        t (future
            (Thread/sleep ms)
            (future-cancel h)
            (deliver p nil))]
    @p))

Requires two threads, but just an idea.

rodnaph
  • 1,237
  • 10
  • 12