Executing a function with a timeout

前端 未结 6 719
醉梦人生
醉梦人生 2020-12-14 19:30

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

(with-timeout 5000
 (do-somthing))

Unless do-so

相关标签:
6条回答
  • 2020-12-14 19:44

    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
    
    0 讨论(0)
  • 2020-12-14 19:45

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

    0 讨论(0)
  • 2020-12-14 19:51

    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
    
    0 讨论(0)
  • 2020-12-14 19:53

    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))
    
    0 讨论(0)
  • 2020-12-14 19:56

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

    0 讨论(0)
  • 2020-12-14 19:58

    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 讨论(0)
提交回复
热议问题