开发者

Executing a function with a timeout

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

(with-timeout 5000
 (do-somthing))开发者_StackOverflow

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.


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


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


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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜